package com.fjhx.purchase.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.entity.purchase.PurchaseContract;
import com.fjhx.purchase.mapper.PurchaseContractMapper;
import com.fjhx.purchase.service.PurchaseContractService;
import org.springblade.core.tool.utils.ObjectUtil;
import org.springblade.core.tool.utils.StringUtil;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 采购合同 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2022-07-14
 */
@Service
public class PurchaseContractServiceImpl extends ServiceImpl<PurchaseContractMapper, PurchaseContract> implements PurchaseContractService {

    /**
     * 校验是否为第一次购买此物料得供应商
     *
     * @param supplierId
     * @param materialCode
     * @return
     */
    @Override
    public Long checkNewSupplier(String supplierId, String materialCode) {
        Long count = baseMapper.selectCount(Wrappers.<PurchaseContract>query().lambda().
                eq(PurchaseContract::getSupplierId, supplierId).
                eq(PurchaseContract::getMaterialCode, materialCode));
        return count;
    }

    /**
     * 上次面料采购
     *
     * @param materialCode
     * @return
     */
    @Override
    public PurchaseContract newPur(String materialCode) {
        return baseMapper.getNewDataByMaterialCode(materialCode);
    }

    /**
     * 合同历史付款记录
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> historyPayList() {
        return baseMapper.historyPayList();
    }

    /**
     * 历史付款列表
     *
     * @param condition
     * @return
     */
    @Override
    public List<PurchaseContract> getPayList(Map<String, Object> condition) {
        List<PurchaseContract> list = baseMapper.getPayList(condition);
        Map<String, Object> map = new HashMap<>();
        //初始化map集合
        map.put("subscribeCheck", true);
        map.put("inoutBillCheck", false);
        map.put("quantityCheck", false);
        map.put("ContractCheck", false);
        map.put("invoiceCheck", false);
        map.put("payDate", null);
        for (PurchaseContract p : list) {
            //处理是否入库判定
            if (ObjectUtil.isNotEmpty(p.getPurContractState()) && p.getPurContractState() == 3) {
                map.put("inoutBillCheck", true);
            }
            //处理是否质检判定
            if (ObjectUtil.isNotEmpty(p.getQuantityStatus())) {
                map.put("quantityCheck", StringUtil.equals(p.getQuantityStatus(), "1"));
            }
            //处理合同判定
            if (ObjectUtil.isNotEmpty(p.getContractStatus()) &&
                    p.getContractStatus().contains("3")) {
                map.put("ContractCheck", true);
            }
            //处理发票判定
            if (ObjectUtil.isNotEmpty(p.getContractAmount())
                    && p.getContractAmount().compareTo(p.getApplyPayAmount()) > -1) {
                map.put("invoiceCheck", true);
            }
            //处理付款期限
            if (ObjectUtil.isNotEmpty(p.getCompleteStockTime())) {
                Date date = DateUtil.offsetDay(DateUtil.parse(p.getCompleteStockTime()), p.getAccountDate());
                map.put("payDate", DateUtil.betweenDay(new Date(), date, true));
            }
            p.setCheck(map);
        }
        return list;
    }

    /**
     * 历史付款列表--条数
     *
     * @param condition
     * @return
     */
    @Override
    public Integer getPayListCount(Map<String, Object> condition) {
        return baseMapper.getPayListCount(condition);
    }

    /**
     * 统计供应商总付款金额
     *
     * @param condition
     * @return
     */
    @Override
    public List<Map<String, Object>> getPayGroupBySu(Map<String, Object> condition) {
        return baseMapper.getPayGroupBySu(condition);
    }

    /**
     * 获取总金额
     *
     * @param date
     * @return
     */
    @Override
    public BigDecimal getSumMoney(String date) {
        return baseMapper.getSumMoney(date);
    }

}
