package com.hc.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hc.business.mapper.ZccmOrdersMapper;
import com.hc.business.mapper.ZccmUserInvoicesMapper;
import com.hc.business.model.ZccmOrders;
import com.hc.business.model.ZccmUserInvoices;
import com.hc.business.service.ZccmUserInvoicesService;
import com.hc.core.utils.status.CodeFlagEnum;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 用户发票表(ZccmUserInvoices)表服务实现类
 *
 * @author java
 * @since 2020-12-02 10:50:38
 */

@Service("userInvoicesService")
public class ZccmUserInvoicesServiceImpl implements ZccmUserInvoicesService {

    private final ZccmUserInvoicesMapper userInvoicesMapper;

    private final ZccmOrdersMapper ordersMapper;

    public ZccmUserInvoicesServiceImpl(ZccmUserInvoicesMapper userInvoicesMapper,
                                       ZccmOrdersMapper ordersMapper) {
        this.userInvoicesMapper = userInvoicesMapper;
        this.ordersMapper = ordersMapper;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param userInvoicesId 主键
     * @return 实例对象
     */
    @Override
    public ZccmUserInvoices queryById(Integer userInvoicesId) {
        return this.userInvoicesMapper.selectByPrimaryKey(userInvoicesId);
    }

    /**
     * 新增数据
     *
     * @param userInvoices 实例对象
     * @return 实例对象
     */
    @Override
    public ZccmUserInvoices insert(ZccmUserInvoices userInvoices) {
        userInvoices.setStatus(CodeFlagEnum.OBJECT_ENABLE_CODE.getCode());
        userInvoices.setCreateTime(System.currentTimeMillis());
        userInvoicesMapper.insert(userInvoices);
        userInvoices.getOrdersIdList().forEach(orderId -> {
            ZccmOrders orders = ordersMapper.selectByPrimaryKey(orderId);
            if (!Objects.isNull(orders)) {
//                Integer orderStatus = orders.getOrderStatus();
//                if (OrdersFlagEnum.ORDERS_FLAG_PAID.getCode().equals(orderStatus)) {
                if (orders.getUserInvoicesId() == null) {
                    orders.setUserInvoicesId(userInvoices.getUserInvoicesId());
                    ordersMapper.updateByPrimaryKey(orders);
//                        userInvoices.setStatus(CodeFlagEnum.OBJECT_ENABLE_CODE.getCode());
//                        update(userInvoices);
//                    }
                }
            }
        });
        return userInvoices;
    }

    /**
     * 修改数据
     *
     * @param userInvoices 实例对象
     * @return 实例对象
     */
    @Override
    public ZccmUserInvoices update(ZccmUserInvoices userInvoices) {
        this.userInvoicesMapper.updateByPrimaryKeySelective(userInvoices);
        return this.queryById(userInvoices.getUserInvoicesId());
    }

    /**
     * 通过主键删除数据
     *
     * @param userInvoicesId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer userInvoicesId) {
        return this.userInvoicesMapper.deleteByPrimaryKey(userInvoicesId) > 0;
    }

    /**
     * 分页查询多条数据
     *
     * @param page 页码
     * @param size 查询条数
     * @return 对象列表
     */
    @Override
    public PageInfo queryAllByLimit(int page, int size) {
        return PageHelper.startPage(page, size).doSelectPageInfo(this.userInvoicesMapper::queryAll);
    }

    /**
     * 依据用户id获取发票信息
     *
     * @param userId 用户id
     * @return 实例对象数组
     */
    @Override
    public List<ZccmUserInvoices> queryDetailByUserId(Integer userId) {
        return userInvoicesMapper.queryDetailByUserId(userId);
    }

    /**
     * 获取班次发票信息
     *
     * @param classId 班次id
     * @return 实例对象数组
     */
    @Override
    public List<ZccmUserInvoices> queryByClassId(Integer classId) {
        return userInvoicesMapper.queryByClassId(classId);
    }

    /**
     * 获取状态获取发票信息
     *
     * @param status 状态
     * @return 实例对象数组
     */
    @Override
    public List<ZccmUserInvoices> queryByStatus(Integer status) {
        return userInvoicesMapper.queryByStatus(status);
    }


}