package vip.xiaonuo.production.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.production.entity.ProductionReceive;
import vip.xiaonuo.production.entity.ProductionWriteRate;
import vip.xiaonuo.production.mapper.ProductionReceiveMapper;
import vip.xiaonuo.production.mapper.ProductionWriteRateMapper;
import vip.xiaonuo.production.param.ProductionReceiveParam;
import vip.xiaonuo.production.service.IProductionReceiveService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 生成管控收率情况 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-12-12
 */
@Service
public class ProductionReceiveServiceImpl extends ServiceImpl<ProductionReceiveMapper, ProductionReceive> implements IProductionReceiveService {

    @Autowired
    private ProductionReceiveMapper receiveMapper;
    @Autowired
    private ProductionWriteRateMapper writeRateMapper;

    @Override
    public List<ProductionReceive> getList(ProductionReceiveParam receiveParam) {
        String locationName = receiveParam.getLocationName();
        String produceName = receiveParam.getProduceName();//可多选
        String orgId = receiveParam.getOrgId();
        Integer timeType = receiveParam.getTimeType();
        Integer number = receiveParam.getNumber();
        QueryWrapper<ProductionWriteRate> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq(StringUtils.isNotBlank(orgId), "org_id", orgId)
                .eq(StringUtils.isNotBlank(locationName), "location_name", locationName);
        if (StringUtils.isNotBlank(produceName)) {
            queryWrapper1.like("production_name", produceName);
        }
        queryWrapper1.select(" convert(IFNULL(avg(production_rate),'0'),decimal(10,2)) productionRate, create_time createTime");
        if (0 == timeType) {
            Calendar calendar = Calendar.getInstance();
            if (null == number) {
                calendar.add(Calendar.YEAR, -3);
            } else {
                calendar.add(Calendar.YEAR, -number);
            }
            queryWrapper1.between("create_time", calendar.getTime(), new Date());
            queryWrapper1.groupBy("DATE_FORMAT(create_time,'%Y')");
        } else {
            Calendar calendar = Calendar.getInstance();
            if (null == number) {
                calendar.add(Calendar.MONTH, -13);
            } else {
                calendar.add(Calendar.MONTH, -number);
            }
            queryWrapper1.between("create_time", calendar.getTime(), new Date());
            queryWrapper1.groupBy("DATE_FORMAT(create_time,'%Y-%m')");
        }
        queryWrapper1.orderByAsc("create_time");
        List<ProductionWriteRate> writeRates = writeRateMapper.selectList(queryWrapper1);
        List<ProductionReceive> list = new ArrayList<>();
        if (writeRates.size() != 0) {
            for (ProductionWriteRate writeRate : writeRates) {
                ProductionReceive receive = new ProductionReceive();
                receive.setRate(writeRate.getProductionRate());
                receive.setCreateTime(writeRate.getCreateTime());
                list.add(receive);
            }
        }
        return list;
    }

}
