package com.huimv.produce.produce.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huimv.common.utils.DataUill;
import com.huimv.common.utils.NumberUtils;
import com.huimv.common.utils.Result;
import com.huimv.common.utils.ResultCode;
import com.huimv.produce.produce.entity.ProdCostStatistics;
import com.huimv.produce.produce.entity.dto.CostDto;
import com.huimv.produce.produce.service.IProdCostStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  成本表
 * </p>
 *
 * @author astupidcoder
 * @since 2021-11-25
 */
@RestController
@RequestMapping("/prodCostStatistics")
public class ProdCostStatisticsController {
    @Autowired
    private IProdCostStatisticsService costStatisticsService;

    @GetMapping("listCost")
    public Result getCost(@RequestParam(name = "farmId") Integer farmId){
        List<ProdCostStatistics> list = costStatisticsService.list(new QueryWrapper<ProdCostStatistics>().eq("farm_id", farmId).eq("add_date", DataUill.getTimesmorning()));
        CostDto costDto1 = new CostDto("基础猪群","0","0","0");
        CostDto costDto2 = new CostDto("保育猪群","0","0","0");
        CostDto costDto3 = new CostDto("育肥猪群","0","0","0");
        CostDto costDto4 = new CostDto("后备猪群","0","0","0");
        CostDto costDto5 = new CostDto("总成本","0","0","0");
        Map map = new HashMap<>();

        for (ProdCostStatistics prodCostStatistics : list) {
            String projectName = prodCostStatistics.getProjectName();

            if ("公摊费用项目".equals(projectName)){
                costDto1.setGtValue(prodCostStatistics.getBaseGroup());
                costDto2.setGtValue(prodCostStatistics.getPigletGroup());
                costDto3.setGtValue(prodCostStatistics.getFattenPig());
                costDto4.setGtValue(prodCostStatistics.getReplaceGilt());
                costDto5.setGtValue(prodCostStatistics.getTotal());

                map.put("gt", NumberUtils.format(prodCostStatistics.getTotal()));
            }
            if ("兽药".equals(projectName)){
                costDto1.setSyValue(prodCostStatistics.getBaseGroup());
                costDto2.setSyValue(prodCostStatistics.getPigletGroup());
                costDto3.setSyValue(prodCostStatistics.getFattenPig());
                costDto4.setSyValue(prodCostStatistics.getReplaceGilt());
                costDto5.setSyValue(prodCostStatistics.getTotal());
                map.put("sy", NumberUtils.format(prodCostStatistics.getTotal()));

            }
            if ("饲料".equals(projectName)){
                costDto1.setSlValue(prodCostStatistics.getBaseGroup());
                costDto2.setSlValue(prodCostStatistics.getPigletGroup());
                costDto3.setSlValue(prodCostStatistics.getFattenPig());
                costDto4.setSlValue(prodCostStatistics.getReplaceGilt());
                costDto5.setSlValue(prodCostStatistics.getTotal());
                map.put("sl", NumberUtils.format(prodCostStatistics.getTotal()));
            }
        }



        map.put("baseGroup",costDto1);
        map.put("pigletGroup",costDto2);
        map.put("fattenPig",costDto3);
        map.put("replaceGilt",costDto4);
        map.put("total",costDto5);




        return new Result(ResultCode.SUCCESS,map);
    }

}
