package com.hc.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hc.business.dto.UserInvoiceDto;
import com.hc.business.mapper.OrdersMapper;
import com.hc.business.mapper.UserInvoicesMapper;
import com.hc.business.model.Orders;
import com.hc.business.model.UserInvoices;
import com.hc.business.service.UserInvoicesService;
import com.hc.core.utils.tool.ResultUtils;
import com.hc.core.utils.tool.TimerUtil;
import com.hc.core.utils.tool.status.CodeFlagEnum;
import org.springframework.stereotype.Service;

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

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

@Service("userInvoicesService")
public class UserInvoicesServiceImpl implements UserInvoicesService {

    private final UserInvoicesMapper userInvoicesMapper;

    private final OrdersMapper ordersMapper;

    public UserInvoicesServiceImpl(UserInvoicesMapper userInvoicesMapper,
                                   OrdersMapper ordersMapper) {
        this.userInvoicesMapper = userInvoicesMapper;
        this.ordersMapper = ordersMapper;
    }

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

    /**
     * 新增数据
     *
     * @param userInvoices 实例对象
     * @return 实例对象
     */
    @Override
    public UserInvoices insert(UserInvoices userInvoices) {
        userInvoices.setStatus(CodeFlagEnum.OBJECT_ENABLE_CODE.getCode());
        userInvoices.setCreateTime(System.currentTimeMillis());
        userInvoicesMapper.insert(userInvoices);
        //userInvoices.getOrdersIdList().forEach(orderId -> {
            Orders orders = ordersMapper.selectByPrimaryKey(userInvoices.getOrderId());
            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 UserInvoices update(UserInvoices 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);
    }

    @Override
    public PageInfo queryAllInvoices(int page, int size,String orderNo,Long begin,Long end) {

        PageHelper.startPage(page, size);
        List<UserInvoiceDto> invoices = this.userInvoicesMapper.queryAllInvoices(orderNo,begin,end);
        for (UserInvoiceDto record : invoices) {
            record.setOrderStatusName(ResultUtils.getOrderStatusName(record.getOrderStatus()));
            record.setCreateTimeStr(TimerUtil.timeStamp2Date(record.getCreateTime()));
        }

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

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


    /**
     * 获取班次发票信息
     *
     * @param orderId 订单ID
     * @param syncStatus nono，发票所处状态-1不开只记录， 0申请中 10申请成功，20开票完成，30开票失败，40开票成功签章失败， 50作废申请，60未作废 70作废中 80作废失败 90作废成功
     * @return 实例对象数组
     */
    @Override
    public List<UserInvoices> queryByOrderId(Integer orderId,Integer syncStatus) {
        return userInvoicesMapper.queryByOrderId(orderId,syncStatus);
    }

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

    /**
     * @Author ymnets
     * @Description 根据流水号查询发票
     * @return com.hc.business.model.UserInvoices
     **/
    @Override
    public UserInvoices queryBySyncSerialNum(String syncSerialNum){
        return userInvoicesMapper.queryBySyncSerialNum(syncSerialNum);
    }

    @Override
    public UserInvoices queryByFPCode(String fpCode){
        return userInvoicesMapper.queryByFPCode(fpCode);
    }

    @Override
    public UserInvoices queryBySyncOrderNo(String syncOrderNo){
        return userInvoicesMapper.queryBySyncOrderNo(syncOrderNo);
    }
}