package com.zzsmart.qomo.kn.cost.manage.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zzsmart.qomo.kn.cost.manage.entity.AppSceneCostMiningDiff;
import com.zzsmart.qomo.kn.cost.manage.entity.AppSceneCostMiningResult;
import com.zzsmart.qomo.kn.cost.manage.mapper.AppSceneCostMiningDiffMapper;
import com.zzsmart.qomo.kn.cost.manage.service.IAppSceneCostMiningDiffService;
import com.zzsmart.qomo.kn.cost.manage.service.IAppSceneCostMiningResultService;
import com.zzsmart.qomo.kn.cost.manage.vo.MiningDiffTotalVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: app_scene_cost_mining_diff
 * @Author: jeecg-boot
 * @Date:   2024-10-15
 * @Version: V1.0
 */
@Service
public class AppSceneCostMiningDiffServiceImpl extends ServiceImpl<AppSceneCostMiningDiffMapper, AppSceneCostMiningDiff> implements IAppSceneCostMiningDiffService {

    @Autowired
    private IAppSceneCostMiningResultService iAppSceneCostMiningResultService;

    @Override
    public List<MiningDiffTotalVO> getMiningDiffTotalById(int id){
        List<MiningDiffTotalVO> miningDiffTotalVOS=new ArrayList<>();
        QueryWrapper<AppSceneCostMiningDiff> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("resultid",id);
        List<AppSceneCostMiningDiff> miningDiffs=this.list(queryWrapper);

        MiningDiffTotalVO materialVo=MiningDiffTotalVO.builder()
                .name("材料成本")
                .diff1101(0)
                .diff1102(0)
                .diff1103(0)
                .diff1104(0)
                .diff1105(0)
                .diff1106(0)
                .diff1199(0)
                .diff2000(0)
                .diff3101(0)
                .diff3201(0)
                .build();

        MiningDiffTotalVO laborVO=MiningDiffTotalVO.builder()
                .name("直接人工")
                .diff1101(0)
                .diff1102(0)
                .diff1103(0)
                .diff1104(0)
                .diff1105(0)
                .diff1106(0)
                .diff1199(0)
                .diff2000(0)
                .diff3101(0)
                .diff3201(0)
                .build();
        MiningDiffTotalVO manuVO=MiningDiffTotalVO.builder()
                .name("制造费用")
                .diff1101(0)
                .diff1102(0)
                .diff1103(0)
                .diff1104(0)
                .diff1105(0)
                .diff1106(0)
                .diff1199(0)
                .diff2000(0)
                .diff3101(0)
                .diff3201(0)
                .build();

        MiningDiffTotalVO totalVO=MiningDiffTotalVO.builder()
                .name("合计")
                .diff1101(0)
                .diff1102(0)
                .diff1103(0)
                .diff1104(0)
                .diff1105(0)
                .diff1106(0)
                .diff1199(0)
                .diff2000(0)
                .diff3101(0)
                .diff3201(0)
                .build();

        AppSceneCostMiningResult miningResult=iAppSceneCostMiningResultService.getById(id);
        if(miningResult!=null){
            materialVo.setRealcost(miningResult.getRealmaterial());
            laborVO.setRealcost(miningResult.getReallabor());
            manuVO.setRealcost(miningResult.getRealproductcost());

            materialVo.setStdcost(miningResult.getStdmaterial());
            laborVO.setStdcost(miningResult.getStdlabor());
            manuVO.setStdcost(miningResult.getStdproductcost());

            materialVo.setDiffcost(materialVo.getRealcost()- materialVo.getStdcost());
            laborVO.setDiffcost(laborVO.getRealcost()- laborVO.getStdcost());
            manuVO.setDiffcost(manuVO.getRealcost()- manuVO.getStdcost());

            totalVO.setRealcost(miningResult.getTotalreal());
            totalVO.setStdcost(miningResult.getTotalstd());
            totalVO.setDiffcost(totalVO.getRealcost()- totalVO.getStdcost());
        }

        //材料
        for (AppSceneCostMiningDiff miningDiff:miningDiffs){
            String type = miningDiff.getType();
            String code = miningDiff.getCode();
            if(type.equals("material")){
                if(code.equals("1101")){
                    materialVo.setDiff1101(materialVo.getDiff1101()+miningDiff.getValue());
                    totalVO.setDiff1101(totalVO.getDiff1101()+miningDiff.getValue());
                }
                if(code.equals("1102")){
                    materialVo.setDiff1102(materialVo.getDiff1102()+miningDiff.getValue());
                    totalVO.setDiff1102(totalVO.getDiff1102()+miningDiff.getValue());
                }
                if(code.equals("1103")){
                    materialVo.setDiff1103(materialVo.getDiff1103()+miningDiff.getValue());
                    totalVO.setDiff1103(totalVO.getDiff1103()+miningDiff.getValue());
                }
                if(code.equals("1104")){
                    materialVo.setDiff1104(materialVo.getDiff1104()+miningDiff.getValue());
                    totalVO.setDiff1104(totalVO.getDiff1104()+miningDiff.getValue());
                }
                if(code.equals("1105")){
                    materialVo.setDiff1105(materialVo.getDiff1105()+miningDiff.getValue());
                    totalVO.setDiff1105(totalVO.getDiff1105()+miningDiff.getValue());
                }
                if(code.equals("1106")){
                    materialVo.setDiff1106(materialVo.getDiff1106()+miningDiff.getValue());
                    totalVO.setDiff1106(totalVO.getDiff1106()+miningDiff.getValue());
                }
                if(code.equals("1199")){
                    materialVo.setDiff1199(materialVo.getDiff1199()+miningDiff.getValue());
                    totalVO.setDiff1199(totalVO.getDiff1199()+miningDiff.getValue());
                }
                if(code.equals("2101")){
                    materialVo.setDiff2000(materialVo.getDiff2000()+miningDiff.getValue());
                    totalVO.setDiff2000(totalVO.getDiff2000()+miningDiff.getValue());
                }
                if(code.equals("2102")){
                    materialVo.setDiff2000(materialVo.getDiff2000()+miningDiff.getValue());
                    totalVO.setDiff2000(totalVO.getDiff2000()+miningDiff.getValue());
                }
                if(code.equals("2103")){
                    materialVo.setDiff2000(materialVo.getDiff2000()+miningDiff.getValue());
                    totalVO.setDiff2000(totalVO.getDiff2000()+miningDiff.getValue());
                }
                if(code.equals("3101")){
                    materialVo.setDiff3101(materialVo.getDiff3101()+miningDiff.getValue());
                    totalVO.setDiff3101(totalVO.getDiff3101()+miningDiff.getValue());
                }
                if(code.equals("3201")){
                    materialVo.setDiff3201(materialVo.getDiff3201()+miningDiff.getValue());
                    totalVO.setDiff3201(totalVO.getDiff3201()+miningDiff.getValue());
                }
            }
            if(type.equals("labor")){
                if(code.equals("1101")){
                    laborVO.setDiff1101(laborVO.getDiff1101()+miningDiff.getValue());
                    totalVO.setDiff1101(totalVO.getDiff1101()+miningDiff.getValue());
                }
                if(code.equals("1102")){
                    laborVO.setDiff1102(laborVO.getDiff1102()+miningDiff.getValue());
                    totalVO.setDiff1102(totalVO.getDiff1102()+miningDiff.getValue());
                }
                if(code.equals("1103")){
                    laborVO.setDiff1103(laborVO.getDiff1103()+miningDiff.getValue());
                    totalVO.setDiff1103(totalVO.getDiff1103()+miningDiff.getValue());
                }
                if(code.equals("1104")){
                    laborVO.setDiff1104(laborVO.getDiff1104()+miningDiff.getValue());
                    totalVO.setDiff1104(totalVO.getDiff1104()+miningDiff.getValue());
                }
                if(code.equals("1105")){
                    laborVO.setDiff1105(laborVO.getDiff1105()+miningDiff.getValue());
                    totalVO.setDiff1105(totalVO.getDiff1105()+miningDiff.getValue());
                }
                if(code.equals("1106")){
                    laborVO.setDiff1106(laborVO.getDiff1106()+miningDiff.getValue());
                    totalVO.setDiff1106(totalVO.getDiff1106()+miningDiff.getValue());
                }
                if(code.equals("1199")){
                    laborVO.setDiff1199(laborVO.getDiff1199()+miningDiff.getValue());
                    totalVO.setDiff1199(totalVO.getDiff1199()+miningDiff.getValue());
                }
                if(code.equals("2101")){
                    laborVO.setDiff2000(laborVO.getDiff2000()+miningDiff.getValue());
                    totalVO.setDiff2000(totalVO.getDiff2000()+miningDiff.getValue());
                }
                if(code.equals("2102")){
                    laborVO.setDiff2000(laborVO.getDiff2000()+miningDiff.getValue());
                    totalVO.setDiff2000(totalVO.getDiff2000()+miningDiff.getValue());
                }
                if(code.equals("2103")){
                    laborVO.setDiff2000(laborVO.getDiff2000()+miningDiff.getValue());
                    totalVO.setDiff2000(totalVO.getDiff2000()+miningDiff.getValue());
                }
                if(code.equals("3101")){
                    laborVO.setDiff3101(laborVO.getDiff3101()+miningDiff.getValue());
                    totalVO.setDiff3101(totalVO.getDiff3101()+miningDiff.getValue());
                }
                if(code.equals("3201")){
                    laborVO.setDiff3201(laborVO.getDiff3201()+miningDiff.getValue());
                    totalVO.setDiff3201(totalVO.getDiff3201()+miningDiff.getValue());
                }
            }
            if(type.equals("product")){
                if(code.equals("1101")){
                    manuVO.setDiff1101(manuVO.getDiff1101()+miningDiff.getValue());
                    totalVO.setDiff1101(totalVO.getDiff1101()+miningDiff.getValue());
                }
                if(code.equals("1102")){
                    manuVO.setDiff1102(manuVO.getDiff1102()+miningDiff.getValue());
                    totalVO.setDiff1102(totalVO.getDiff1102()+miningDiff.getValue());
                }
                if(code.equals("1103")){
                    manuVO.setDiff1103(manuVO.getDiff1103()+miningDiff.getValue());
                    totalVO.setDiff1103(totalVO.getDiff1103()+miningDiff.getValue());
                }
                if(code.equals("1104")){
                    manuVO.setDiff1104(manuVO.getDiff1104()+miningDiff.getValue());
                    totalVO.setDiff1104(totalVO.getDiff1104()+miningDiff.getValue());
                }
                if(code.equals("1105")){
                    manuVO.setDiff1105(manuVO.getDiff1105()+miningDiff.getValue());
                    totalVO.setDiff1105(totalVO.getDiff1105()+miningDiff.getValue());
                }
                if(code.equals("1106")){
                    manuVO.setDiff1106(manuVO.getDiff1106()+miningDiff.getValue());
                    totalVO.setDiff1106(totalVO.getDiff1106()+miningDiff.getValue());
                }
                if(code.equals("1199")){
                    manuVO.setDiff1199(manuVO.getDiff1199()+miningDiff.getValue());
                    totalVO.setDiff1199(totalVO.getDiff1199()+miningDiff.getValue());
                }
                if(code.equals("2101")){
                    manuVO.setDiff2000(manuVO.getDiff2000()+miningDiff.getValue());
                    totalVO.setDiff2000(totalVO.getDiff2000()+miningDiff.getValue());
                }
                if(code.equals("2102")){
                    manuVO.setDiff2000(manuVO.getDiff2000()+miningDiff.getValue());
                    totalVO.setDiff2000(totalVO.getDiff2000()+miningDiff.getValue());
                }
                if(code.equals("2103")){
                    manuVO.setDiff2000(manuVO.getDiff2000()+miningDiff.getValue());
                    totalVO.setDiff2000(totalVO.getDiff2000()+miningDiff.getValue());
                }
                if(code.equals("3101")){
                    manuVO.setDiff3101(manuVO.getDiff3101()+miningDiff.getValue());
                    totalVO.setDiff3101(totalVO.getDiff3101()+miningDiff.getValue());
                }
                if(code.equals("3201")){
                    manuVO.setDiff3201(manuVO.getDiff3201()+miningDiff.getValue());
                    totalVO.setDiff3201(totalVO.getDiff3201()+miningDiff.getValue());
                }
            }
        }

        miningDiffTotalVOS.add(materialVo);
        miningDiffTotalVOS.add(laborVO);
        miningDiffTotalVOS.add(manuVO);
        miningDiffTotalVOS.add(totalVO);

        return miningDiffTotalVOS;
    }
}
