package cn.cecep.talroad.controller.effective;

import cn.cecep.talroad.common.core.utils.bean.BeanUtils;
import cn.cecep.talroad.common.core.web.domain.AjaxResults;
import cn.cecep.talroad.domain.effective.MGovernanceTechnologyEstimateFacilities;
import cn.cecep.talroad.domain.effective.MGovernanceTechnologyEstimateFactory;
import cn.cecep.talroad.domain.effective.MGovernanceTechnologyEstimateProcedure;
import cn.cecep.talroad.service.effective.MGovernanceTechnologyEstimateFacilitiesService;
import cn.cecep.talroad.service.effective.MGovernanceTechnologyEstimateFactoryService;
import cn.cecep.talroad.service.effective.MGovernanceTechnologyEstimateProcedureService;
import cn.cecep.talroad.vo.effective.MGovernanceTechnologyEstimate;
import cn.cecep.talroad.vo.query.effective.MGovernanceTechnologyEstimateQuery;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
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.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Api(tags = "治理技术评估")
@RestController
@RequestMapping("governanceEstimate")
public class MGovernanceTechnologyEstimateController {

    @Autowired
    private MGovernanceTechnologyEstimateFactoryService factoryService;

    @Autowired
    private MGovernanceTechnologyEstimateFacilitiesService facilitiesService;

    @Autowired
    private MGovernanceTechnologyEstimateProcedureService procedureService;

    @GetMapping("list")
    @ApiOperation("获取列表")
    public AjaxResults<IPage<MGovernanceTechnologyEstimate>> getList(MGovernanceTechnologyEstimateQuery query){
        IPage<MGovernanceTechnologyEstimate> result=new Page<>();
        if(query.getAnalyzeLatitude()==1) {
            Page<MGovernanceTechnologyEstimateFactory> p = new Page<>(query.getPageNum(), query.getPageSize());
            IPage<MGovernanceTechnologyEstimateFactory> page = factoryService.page(p, MGovernanceTechnologyEstimateQuery.createFactoryWrapper(query));
            BeanUtils.copyBeanProp(result, page);
        }else if(query.getAnalyzeLatitude()==2) {
            Page<MGovernanceTechnologyEstimateFacilities> p = new Page<>(query.getPageNum(), query.getPageSize());
            IPage<MGovernanceTechnologyEstimateFacilities> page = facilitiesService.page(p, MGovernanceTechnologyEstimateQuery.createFacilitiesWrapper(query));
            BeanUtils.copyBeanProp(result, page);
        }
        else if(query.getAnalyzeLatitude()==3) {
            Page<MGovernanceTechnologyEstimateProcedure> p = new Page<>(query.getPageNum(), query.getPageSize());
            IPage<MGovernanceTechnologyEstimateProcedure> page = procedureService.page(p, MGovernanceTechnologyEstimateQuery.createProcedureWrapper(query));
            BeanUtils.copyBeanProp(result, page);
        }
        return AjaxResults.success(result);
    }
    @GetMapping("selectList")
    @ApiOperation("模糊筛选字段查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "analyzeLatitude",value = "必填：分析纬度  1==企业维度，2==治理设施维度，3==工序维度"),
            @ApiImplicitParam(name = "value",value = "模糊查询时传递的数据"),
            @ApiImplicitParam(name = "name",value = "必填：查询的数据 (企业名：factory_name，行业：industry_category，治理技术：governance_technology" +
                    "评估结果：administer_results，治理设施：governance_facilities，工序：procedure)")
    })
    public AjaxResults<List<String>> getList(Integer analyzeLatitude,String name,String value){
        return AjaxResults.success(factoryService.selectNameByAnalyzeLatitude(analyzeLatitude,name,value));
    }
    @GetMapping("map")
    @ApiOperation("获取图表")
    public AjaxResults<Map<String,Object>> getEChartsVO(MGovernanceTechnologyEstimateQuery query){
        Map<String,Object> map=new HashMap<>();

        List<String> xList=new ArrayList<>();
        List<BigDecimal> yList = new ArrayList<>();
        StringBuffer selectSql=new StringBuffer(query.getSelectName().replaceAll("([A-Z]+)", "_$1").toLowerCase()+" AS id");
        if(query.getAnalyzeLatitude()==1) {
            selectSql.append(" ,factory_name ");
            selectSql.append(" ,governance_technology ");
            List<MGovernanceTechnologyEstimateFactory> list = factoryService.list(
                    MGovernanceTechnologyEstimateQuery.createFactoryWrapper(query).select(selectSql.toString()));
            xList= list.stream()
                    .map(obj -> obj.getFactoryName() + obj.getGovernanceTechnology())
                    .collect(Collectors.toList());
            yList= list.stream().map(factory -> factory.getId() != null ? factory.getId() : "0").map(BigDecimal::new).collect(Collectors.toList());

        }else if(query.getAnalyzeLatitude()==2) {
            selectSql.append(" ,factory_name ");
            selectSql.append(" ,governance_facilities ");
            List<MGovernanceTechnologyEstimateFacilities> list = facilitiesService.list(
                    MGovernanceTechnologyEstimateQuery.createFacilitiesWrapper(query).select(selectSql.toString()));
            yList= list.stream().map(factory -> factory.getId() != null ? factory.getId() : "0") .map(BigDecimal::new) .collect(Collectors.toList());
            xList= list.stream()
                    .map(obj -> obj.getGovernanceFacilities() + obj.getFactoryName())
                    .collect(Collectors.toList());
        }
        else if(query.getAnalyzeLatitude()==3) {
            selectSql.append(" ,\"procedure\" ");
            selectSql.append(" ,governance_technology ");
            List<MGovernanceTechnologyEstimateProcedure> list = procedureService.list(
                    MGovernanceTechnologyEstimateQuery.createProcedureWrapper(query).select(selectSql.toString()));
            yList= list.stream().map(factory -> factory.getId() != null ? factory.getId() : "0")  .map(BigDecimal::new).collect(Collectors.toList());
            xList= list.stream()
                    .map(obj -> obj.getProcedure() + obj.getGovernanceTechnology())
                    .collect(Collectors.toList());
        }
        map.put("x",xList);

        map.put("y",yList);
        double average = yList.stream()
                .mapToDouble(BigDecimal::doubleValue)
                .average()
                .orElse(0.0);
        map.put("average",average);

        return AjaxResults.success(map);
    }
}
