package vip.xiaonuo.purchase.service.impl;

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.PurchasePrice;
import vip.xiaonuo.purchase.entity.vo.PurchasePriceVo;
import vip.xiaonuo.purchase.mapper.PurchasePriceMapper;
import vip.xiaonuo.purchase.param.PurchasePriceParam;
import vip.xiaonuo.purchase.service.IPurchasePriceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 采购单品价格表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-11-27
 */
@Service
public class PurchasePriceServiceImpl extends ServiceImpl<PurchasePriceMapper, PurchasePrice> implements IPurchasePriceService {

    @Autowired
    private PurchasePriceMapper priceMapper;

    @Override
    public List<PurchasePriceVo> getList(PurchasePriceParam priceParam) {
        QueryWrapper<PurchasePrice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(priceParam.getOrgId()), "org_id", priceParam.getOrgId());
        queryWrapper.select(" CONVERT(IFNULL(avg(price),'0'),decimal(10,2)) 'price',create_time createTime");
        Integer timeType = priceParam.getTimeType();
        List<PurchasePriceVo> list = new ArrayList<>();
        if (null == timeType || 0 == timeType) {
            if ("".equals(priceParam.getMaterial()) || null == priceParam.getMaterial()) {
                queryWrapper.groupBy("DATE_FORMAT(create_time,'%Y')");
            } else {
                queryWrapper.groupBy("DATE_FORMAT(create_time,'%Y')" + "," + priceParam.getMaterial());
            }
            List<PurchasePrice> prices = priceMapper.selectList(queryWrapper);
            if (prices.size() != 0) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
                for (PurchasePrice price : prices) {
                    PurchasePriceVo vo = new PurchasePriceVo();
                    vo.setAvgPrice(price.getPrice());
                    vo.setTime(sdf.format(price.getCreateTime()));
                    list.add(vo);
                }
            }
        } else {
            queryWrapper.ge("create_time", DateUtil.beginOfYear(new Date()));
            if ("".equals(priceParam.getMaterial()) || null == priceParam.getMaterial()) {
                queryWrapper.groupBy("DATE_FORMAT(create_time,'%Y-%m')");
            } else {
                queryWrapper.groupBy("DATE_FORMAT(create_time,'%Y-%m')" + "," + priceParam.getMaterial());
            }
            QueryWrapper<PurchasePrice> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq(StringUtils.isNotBlank(priceParam.getOrgId()), "org_id", priceParam.getOrgId());
            queryWrapper1.select(" CONVERT(IFNULL(avg(price),'0'),decimal(10,2)) 'price'");
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.YEAR, -1);
            int lastYear = calendar.get(Calendar.YEAR);
            queryWrapper1.between("create_time", lastYear + "-01-01 00:00:00", lastYear + "-12-31 23:59:59");
            PurchasePrice purchasePrice = priceMapper.selectOne(queryWrapper1);
            List<PurchasePrice> prices = priceMapper.selectList(queryWrapper);
            if (prices.size() != 0) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
                for (PurchasePrice price : prices) {
                    PurchasePriceVo vo = new PurchasePriceVo();
                    vo.setAvgPrice(price.getPrice());
                    vo.setLastAvgPrice(purchasePrice.getPrice());
                    vo.setTime(sdf.format(price.getCreateTime()));
                    list.add(vo);
                }
            }
        }
        return list;
    }

}
