package vip.xiaonuo.purchase.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import vip.xiaonuo.purchase.entity.PurchaseContract;
import vip.xiaonuo.purchase.entity.PurchaseOrder;
import vip.xiaonuo.purchase.entity.vo.PurchaseContractVo;
import vip.xiaonuo.purchase.entity.vo.PurchaseOrderMoneyVo;
import vip.xiaonuo.purchase.mapper.PurchaseContractMapper;
import vip.xiaonuo.purchase.param.PurchaseOrderParam;
import vip.xiaonuo.purchase.service.IPurchaseContractService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 采购合同 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-11-27
 */
@Service
public class PurchaseContractServiceImpl extends ServiceImpl<PurchaseContractMapper, PurchaseContract> implements IPurchaseContractService {

    @Autowired
    private PurchaseContractMapper contractMapper;

    @Override
    public List<PurchaseContractVo> getList(PurchaseOrderParam orderParam) {
        String orgId = orderParam.getOrgId();
        QueryWrapper<PurchaseContract> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId).orderByAsc("create_time");
        queryWrapper.select(" IFNULL(sum(contract_money),'0') contractMoney,create_time createTime,IFNULL(count(*),'0') materialNumber");
        BigDecimal math = new BigDecimal("10000");

        List<PurchaseContractVo> list = new ArrayList<>();
        if (null == orderParam.getTimeType()||0==orderParam.getTimeType()) {
            if ("".equals(orderParam.getOrderType()) || null == orderParam.getOrderType()) {
                queryWrapper.groupBy("DATE_FORMAT(create_time,'%Y')");
            } else {
                queryWrapper.groupBy("DATE_FORMAT(create_time,'%Y')" + "," + orderParam.getOrderType());
            }
            List<PurchaseContract> contracts = contractMapper.selectList(queryWrapper);
            if (contracts.size() != 0) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
                for (PurchaseContract contract : contracts) {
                    PurchaseContractVo vo = new PurchaseContractVo();
                    BigDecimal bd = new BigDecimal(contract.getContractMoney());
                    bd = bd.divide(math, 2, RoundingMode.HALF_UP);
                    vo.setContractMoney(bd.toString());
                    vo.setMaterialNumber(contract.getMaterialNumber());
                    vo.setTime(sdf.format(contract.getCreateTime()));
                    list.add(vo);
                }
            }
        } else {
            queryWrapper.ge("create_time",  DateUtil.beginOfYear(new Date()));
            if ("".equals(orderParam.getOrderType()) || null == orderParam.getOrderType()) {
                queryWrapper.groupBy("DATE_FORMAT(create_time,'%Y-%m')");
            } else {
                queryWrapper.groupBy("DATE_FORMAT(create_time,'%Y-%m')" + "," + orderParam.getOrderType());
            }
            List<PurchaseContract> contracts = contractMapper.selectList(queryWrapper);
            if (contracts.size() != 0) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
                for (PurchaseContract contract : contracts) {
                    PurchaseContractVo vo = new PurchaseContractVo();
                    BigDecimal bd = new BigDecimal(contract.getContractMoney());
                    bd = bd.divide(math, 2, RoundingMode.HALF_UP);
                    vo.setContractMoney(bd.toString());
                    vo.setMaterialNumber(contract.getMaterialNumber());
                    vo.setTime(sdf.format(contract.getCreateTime()));
                    list.add(vo);
                }
            }
        }
        return list;
    }
}
