package com.jdc.jdcproject.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jdc.jdcproject.entity.DicekeMiningloss;
import com.jdc.jdcproject.entity.DicekePlatearea;
import com.jdc.jdcproject.entity.DicekeShovel;
import com.jdc.jdcproject.entity.VO.DicekeMininglossVo;
import com.jdc.jdcproject.mapper.DicekeMininglossMapper;
import com.jdc.jdcproject.mapper.DicekePlateareaMapper;
import com.jdc.jdcproject.mapper.DicekeShovelMapper;
import com.jdc.jdcproject.service.IDicekeMininglossService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jdc.jdcproject.service.IDicekePlateareaService;
import com.jdc.jdcproject.service.IDicekeShovelService;
import com.jdc.jdcproject.utils.Result;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 * 采剥与贫损表; 服务实现类
 * </p>
 *
 * @author haoyanlu
 * @since 2025-04-26
 */
@Service
public class DicekeMininglossServiceImpl extends ServiceImpl<DicekeMininglossMapper, DicekeMiningloss> implements IDicekeMininglossService {

    @Autowired
    private DicekeMininglossMapper dicekeMininglossMapper;
    @Autowired
    private DicekeShovelMapper dicekeShovelMapper;
    @Autowired
    private DicekePlateareaMapper dicekePlateareaMapper;

    @Override
    public List<DicekeMininglossVo> findAllLoss() {
        return baseMapper.findAllLoss();
    }

/*
// 两个时间内查询
    @Override
    public List<DicekeMininglossVo> getLossByMonthRange(LocalDate month, LocalDate endMonth) {

        boolean isBefore = month.isBefore(endMonth);
        if (isBefore){

            int SmonthY = month.getYear();
            int SmonthM = month.getMonthValue();
            int endMonthY = endMonth.getYear();
            int endmonthM = endMonth.getMonthValue();
            return baseMapper.findLossByMonthRange(SmonthM,SmonthY,endmonthM,endMonthY);

        } else {

            LocalDate flagMonth;
            flagMonth = month;
            month = endMonth;
            endMonth = flagMonth;

            int monthY = month.getYear();
            int monthM = month.getMonthValue();
            int endMonthY = endMonth.getYear();
            int endMonthM = endMonth.getMonthValue();

            return baseMapper.findLossByMonthRange(monthM,monthY,endMonthM,endMonthY);
        }

    }
*/


    @Override
    public List<DicekeMininglossVo> getLossByMonth(LocalDate month) {
        int year = month.getYear();
        int monthV = month.getMonthValue();
        return baseMapper.findLossByMonth(monthV,year);
    }

    @Override
    public int savedml(DicekeMininglossVo dicekeMininglossVo) {
         /*
        QueryWrapper<DicekePlatearea> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("plateRange",dicekeMininglossVo.getPlateRange());
        DicekePlatearea dp = dicekePlateareaService.getOne(queryWrapper);
        */

        //平盘和电铲查找是否单独一个工具类(如果使用次数多)
        DicekePlatearea dp = dicekePlateareaMapper.selectOne(
                new QueryWrapper<DicekePlatearea>().eq("plateRange", dicekeMininglossVo.getPlateRange())
        );
        System.out.println(dp);
        if (dp == null){
            return 1;
        }

        //不确定是否是数据库表的问题 报错
        DicekeShovel ds = dicekeShovelMapper.selectOne(
                new QueryWrapper<DicekeShovel>().eq("shovelCode", dicekeMininglossVo.getShovelCode())
        );
        System.out.println(ds);
        if (ds == null){
        //    return Result.errorResult().data("电铲不存在",ds);
           return 2;
        }

        //构建报表实体
        //Loss的id生成
        DicekeMiningloss dml = new DicekeMiningloss();
        dml.setShovelID(ds.getShovelID());
        dml.setPlateID(dp.getPlateID());
        dml.setMonth(dicekeMininglossVo.getMonth());
        dml.setDefineMineralReserves(dicekeMininglossVo.getDefineMineralReserves());
        dml.setStrippingAndTotalMiningTon(dicekeMininglossVo.getStrippingAndTotalMiningTon());
        dml.setWasteRockTon(dicekeMininglossVo.getWasteRockTon());
        dml.setDilutionRate(dicekeMininglossVo.getDilutionRate());
        dml.setLossRate(dicekeMininglossVo.getLossRate());
        dml.setAllocationTon(dicekeMininglossVo.getAllocationTon());
        dicekeMininglossMapper.insert(dml);
        return 20000;
    }



}