package com.ruoyi.buyerapi.service.impl;

import java.util.List;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.ruoyi.buyerapi.domain.BuyerFileInfo;
import com.ruoyi.buyerapi.domain.BuyerGoods;
import com.ruoyi.buyerapi.domain.BuyerOrderRecord;
import com.ruoyi.buyerapi.mapper.BuyerFileInfoMapper;
import com.ruoyi.buyerapi.mapper.BuyerOrderRecordMapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.buyerapi.mapper.BuyerBillMapper;
import com.ruoyi.buyerapi.domain.BuyerBill;
import com.ruoyi.buyerapi.service.IBuyerBillService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 系统发票Service业务层处理
 *
 * @author ruoyi
 * @date 2022-09-19
 */
@Service
public class BuyerBillServiceImpl implements IBuyerBillService {
    @Autowired
    private BuyerBillMapper buyerBillMapper;

    @Autowired
    private BuyerOrderRecordMapper buyerOrderRecordMapper;

    @Autowired
    private BuyerFileInfoMapper buyerFileInfoMapper;

    /**
     * 查询系统发票
     *
     * @param id 系统发票主键
     * @return 系统发票
     */
    @Transactional
    @Override
    public BuyerBill selectBuyerBillById(String id) {
        BuyerBill buyerBill = buyerBillMapper.selectBuyerBillById(id);
        // 查询关联文件
        List<BuyerFileInfo> buyerFileInfos = buyerFileInfoMapper.selectBuyerFileInfoByLinkId(id);
        buyerBill.setBuyerFileInfos(buyerFileInfos);
        return buyerBillMapper.selectBuyerBillById(id);
    }

    /**
     * 查询系统发票列表
     *
     * @param buyerBill 系统发票
     * @return 系统发票
     */
    @Override
    public List<BuyerBill> selectBuyerBillList(BuyerBill buyerBill) {
        List<BuyerBill> buyerBills = buyerBillMapper.selectBuyerBillList(buyerBill);
        // 查询关联文件
        for (BuyerBill bill : buyerBills) {
            List<BuyerFileInfo> buyerFileInfos = buyerFileInfoMapper.selectBuyerFileInfoByLinkId(bill.getId());
            bill.setBuyerFileInfos(buyerFileInfos);
        }
        return buyerBills;
    }

    /**
     * 查询发票申请数量
     *
     * @return 发票申请数量
     */
    @Override
    public int selectCountBuyerBillApply() {
        return buyerBillMapper.selectCountBuyerBillApply();
    }

    /**
     * 新增系统发票
     *
     * @param buyerBill 系统发票
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBuyerBill(BuyerBill buyerBill) {
        buyerBill.setCreateTime(DateUtils.getNowDate());
        buyerBill.setApplyTime(DateUtil.now());
        // 新增关联文件
        List<BuyerFileInfo> buyerFileInfos = buyerBill.getBuyerFileInfos();
        if (buyerFileInfos != null && buyerFileInfos.size() > 0) {
            for (BuyerFileInfo buyerFileInfo : buyerFileInfos) {
                buyerFileInfo.setId(IdUtil.simpleUUID());
                buyerFileInfo.setCreateTime(DateUtils.getNowDate());
                buyerFileInfo.setCreateBy(SecurityUtils.getUsername());
                buyerFileInfo.setLinkId(buyerBill.getId());
            }
            buyerFileInfoMapper.batchInsertBuyerFileInfo(buyerFileInfos);
        }
        return buyerBillMapper.insertBuyerBill(buyerBill);
    }

    /**
     * 修改系统发票
     *
     * @param buyerBill 系统发票
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBuyerBill(BuyerBill buyerBill) {
        buyerBill.setUpdateTime(DateUtils.getNowDate());
        // 修改关联文件
        List<BuyerFileInfo> buyerFileInfos = buyerBill.getBuyerFileInfos();
        if (buyerFileInfos != null && buyerFileInfos.size() > 0) {
            for (BuyerFileInfo buyerFileInfo : buyerFileInfos) {
                buyerFileInfo.setId(IdUtil.simpleUUID());
                buyerFileInfo.setUpdateTime(DateUtils.getNowDate());
                buyerFileInfo.setUpdateBy(SecurityUtils.getUsername());
                buyerFileInfo.setLinkId(buyerBill.getId());
            }
            buyerFileInfoMapper.deleteBuyerFileInfoByLinkId(buyerBill.getId());
            buyerFileInfoMapper.batchInsertBuyerFileInfo(buyerFileInfos);
        }
        // 修改平台端订单记录
        BuyerOrderRecord buyerOrderRecord = new BuyerOrderRecord();
        buyerOrderRecord.setId(buyerBill.getOrderId());
        buyerOrderRecord.setBillStatus("2");
        buyerOrderRecordMapper.updateBuyerOrderRecord(buyerOrderRecord);
        // 修改发票
        return buyerBillMapper.updateBuyerBill(buyerBill);
    }

    /**
     * 批量删除系统发票
     *
     * @param ids 需要删除的系统发票主键
     * @return 结果
     */
    @Override
    public int deleteBuyerBillByIds(String[] ids) {
        return buyerBillMapper.deleteBuyerBillByIds(ids);
    }

    /**
     * 删除系统发票信息
     *
     * @param id 系统发票主键
     * @return 结果
     */
    @Override
    public int deleteBuyerBillById(String id) {
        return buyerBillMapper.deleteBuyerBillById(id);
    }
}
