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.mapper.ProductionReceiveMapper;
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.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;

    @Override
    public List<ProductionReceive> getList(ProductionReceiveParam receiveParam) {
        String locationName = receiveParam.getLocationName();
        String produceName = receiveParam.getProduceName();//可多选
        String orgId = receiveParam.getOrgId();
        Integer timeType = receiveParam.getTimeType();
        QueryWrapper<ProductionReceive> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId)
                .eq(StringUtils.isNotBlank(locationName), "location_name", locationName);
        if (StringUtils.isNotBlank(produceName)) {
            queryWrapper.in("produce_name", produceName);
        }
        queryWrapper.select(" convert(IFNULL(avg(rate),'0'),decimal(10,2)) rate,create_time createTime");
        if (0 == timeType) {
            queryWrapper.groupBy("DATE_FORMAT(create_time,'%Y')");
        } else {
            queryWrapper.ge("create_time", DateUtil.beginOfYear(new Date()));
            queryWrapper.groupBy("DATE_FORMAT(create_time,'%Y-%m')");
        }
        queryWrapper.orderByAsc("create_time");
        return receiveMapper.selectList(queryWrapper);
    }

}
