package cn.gyee.tamplate.controller.operation;

import cn.gyee.tamplate.model.auto.Operationrecord;
import cn.gyee.tamplate.model.operation.Operation;
import cn.gyee.tamplate.model.vo.OpeVo;
import cn.gyee.tamplate.service.operation.OperationService;
import cn.gyee.tamplate.util.common.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @ClassName : OperationController
 * @Author : xieshengjie
 * @Date: 2021/1/18 16:02
 * @Description : 操作controller
 */
@RestController
@RequestMapping("//operation")
public class OperationController {
    @Resource
    private OperationService operationService;




    @GetMapping("/listplus")
    @ResponseBody
    @CrossOrigin(origins = "*", maxAge = 3600)
    public List<Operation> listplus(@RequestParam(value = "beginDate",required = true)String beginDate,
                                @RequestParam(value = "endDate",required = true)String endDate){
        List<Operation> resultList =  operationService.findOperecords(beginDate,endDate);

        return resultList;
    }

    @GetMapping("/drillplus")
    @ResponseBody
    @CrossOrigin(origins = "*", maxAge = 3600)
    public List<Operationrecord>  drillplus(@RequestParam(value = "beginDate",required = true)String beginDate,
                              @RequestParam(value = "endDate",required = true)String endDate,
                              @RequestParam(value = "dutyname",required = true)String dutyname,
                              @RequestParam(value = "direct",required = true)String direct){
        List<Operationrecord> collect = operationService.drillOperecords(beginDate, endDate, dutyname, direct);
        return collect;


    }
    @GetMapping("/list")
    @ResponseBody
    @CrossOrigin(origins = "*", maxAge = 3600)
    public List<Operation> list(@RequestParam(value = "beginDate",required = true)String beginDate,
                                @RequestParam(value = "endDate",required = true)String endDate){
        Map<String,List<OpeVo>> opeMap = null;
        List<Operation> resultList = new ArrayList<>();
        try {
            opeMap  =  operationService.opeMap(beginDate,endDate);
            if (StringUtils.isNotEmpty(opeMap)){
                Set<String> dutynames = opeMap.keySet();
                for (String dutyname : dutynames) {
                    Operation ope = new Operation();
                    ope.setNameOfDuty(dutyname);
                    List<OpeVo> opeVos = opeMap.get(dutyname);
                    ope.setStartCount(opeVos.stream().filter(vo -> vo.getValue()==1.0).collect(Collectors.toList()).size());
                    ope.setStopCount(opeVos.stream().filter(vo -> vo.getValue()==2.0).collect(Collectors.toList()).size());
                    ope.setResetCount(opeVos.stream().filter(vo -> vo.getValue()==3.0).collect(Collectors.toList()).size());
                    ope.setMaintainCount(opeVos.stream().filter(vo -> vo.getValue()==4.0).collect(Collectors.toList()).size());
                    ope.setUnmaintainCount(opeVos.stream().filter(vo -> vo.getValue()==5.0).collect(Collectors.toList()).size());
                    ope.setGpmaintainCount(opeVos.stream().filter(vo -> vo.getValue()==6.0).collect(Collectors.toList()).size());
                    ope.setGpfaultCount(opeVos.stream().filter(vo -> vo.getValue()==7.0).collect(Collectors.toList()).size());
                    ope.setGponsiteinvolvementMaintainCount(opeVos.stream().filter(vo -> vo.getValue()==8.0).collect(Collectors.toList()).size());
                    ope.setGponsiteinvolvementFaultCount(opeVos.stream().filter(vo -> vo.getValue()==9.0).collect(Collectors.toList()).size());
                    ope.setGpoffsiteaffectedgridCount(opeVos.stream().filter(vo -> vo.getValue()==100).collect(Collectors.toList()).size());
                    ope.setGpweatherOutsideCount(opeVos.stream().filter(vo -> vo.getValue()==11.0).collect(Collectors.toList()).size());
                    ope.setUngpCount(opeVos.stream().filter(vo -> vo.getValue()==12.0).collect(Collectors.toList()).size());
                    resultList.add(ope);
                }

            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return resultList;


    }


    @GetMapping("/drill")
    @ResponseBody
    @CrossOrigin(origins = "*", maxAge = 3600)
    public List<OpeVo>  drill(@RequestParam(value = "beginDate",required = true)String beginDate,
                                @RequestParam(value = "endDate",required = true)String endDate,
                                 @RequestParam(value = "dutyname",required = true)String dutyname,
                                 @RequestParam(value = "direct",required = true)String direct){
        Map<String,List<OpeVo>> opeMap = null;
        List<OpeVo> collect = null;
        try {
            opeMap  =  operationService.opeMap(beginDate,endDate);
            collect = opeMap.get(dutyname).stream().filter(vo -> vo.getValue() == Double.parseDouble(direct)).collect(Collectors.toList());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return collect;


    }
}
