package com.cpp.web.controller.regulation;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cpp.common.core.domain.R;
import com.cpp.common.core.domain.entity.SysDictData;
import com.cpp.common.core.domain.model.LoginUser;
import com.cpp.common.utils.SecurityUtils;
import com.cpp.system.service.ISysDictDataService;
import com.cpp.web.domain.regulation.TempShortRegulation;
import com.cpp.web.domain.regulation.TempShortUsual;
import com.cpp.web.domain.regulation.TempShortUsualDetail;
import com.cpp.web.domain.station.NwpStation;
import com.cpp.web.dto.DqUsualDto;
import com.cpp.web.dto.TempShortRegulationDto;
import com.cpp.web.service.regulation.TempShortUsualDetailService;
import com.cpp.web.service.regulation.TempShortUsualService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;


/**
 * 短期调控常用策略
 *
 * @author whc
 * @date 2022-03-18 15:49:14
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/dqUsualController")
public class DqUsualController {
    @Autowired
    TempShortUsualService tempShortUsualService;
    @Autowired
    TempShortUsualDetailService tempShortUsualDetailService;
    @Autowired
    ISysDictDataService iSysDictDataService;

    /**
     * 保存常用策略
     * @param dqUsualDto
     * @return
     */
    @PostMapping("/saveUsual")
    public R saveUsual(@RequestBody DqUsualDto dqUsualDto) {
        String usualName = dqUsualDto.getUsualName();
        // 判断名称是否存在
        QueryWrapper<TempShortUsual> tempShortUsualQueryWrapper = new QueryWrapper<>();
        tempShortUsualQueryWrapper.eq("usual_name", usualName);
        TempShortUsual tempShortUsual = tempShortUsualService.getOne(tempShortUsualQueryWrapper);
        if (tempShortUsual!=null){
            return R.fail("常用名称存在，请换个名称！");
        }
        LoginUser loginUser = SecurityUtils.getLoginUser();
        // 保存常用名称
        TempShortUsual tempShortUsual1 = new TempShortUsual();
        tempShortUsual1.setUsualName(usualName);
        tempShortUsual1.setCreateBy(loginUser.getUsername());
        tempShortUsualService.save(tempShortUsual1);


        List<TempShortUsualDetail> tempShortUsualDetailList = new ArrayList<>();
        // 封装常用实体
        List<TempShortRegulationDto> tempShortRegulationDtoList = dqUsualDto.getTempShortRegulationDtoList();
        for (TempShortRegulationDto tempShortRegulationDto:tempShortRegulationDtoList){
            TempShortUsualDetail tempShortUsualDetail = new TempShortUsualDetail();
            tempShortUsualDetail.setUsualId(tempShortUsual1.getId());
            tempShortUsualDetail.setTimePoint(tempShortRegulationDto.getTime());
            tempShortUsualDetail.setSz(tempShortRegulationDto.getSz());
            tempShortUsualDetail.setXs(tempShortRegulationDto.getXs());
            tempShortUsualDetail.setCreateBy(loginUser.getUsername());
            tempShortUsualDetailList.add(tempShortUsualDetail);
        }
        tempShortUsualDetailService.saveBatch(tempShortUsualDetailList);
        return R.ok();
    }
    @PostMapping("/deleteUsual")
    public R deleteUsual(String usualName) {
        QueryWrapper<TempShortUsual> tempShortUsualQueryWrapper = new QueryWrapper<>();
        tempShortUsualQueryWrapper.eq("usual_name", usualName);
        TempShortUsual tempShortUsual = tempShortUsualService.getOne(tempShortUsualQueryWrapper);
        long id = tempShortUsual.getId();
        // 先删除子表数据
        tempShortUsualDetailService.remove(new QueryWrapper<TempShortUsualDetail>().eq("usual_id", id));
        // 再删除主表数据
        tempShortUsualService.removeById(id);

        return R.ok();
    }

    /**
     * 获取常用策略列表
     * @return
     */
    @GetMapping("/getUsualList")
    public R getUsualList() {
        Map map = new HashMap();
        List<TempShortUsual> tempShortUsualList = tempShortUsualService.list();
        // 创建时间倒排序
        tempShortUsualList.sort(Comparator.comparing(TempShortUsual::getCreateTime).reversed());
        map.put("tempShortUsualList",tempShortUsualList);
        // 获取中心侧模型算法
        SysDictData sysDictData = new SysDictData();
        sysDictData.setDictType("forecast_model");
        List<SysDictData> forecastModelList = iSysDictDataService.selectDictDataList(sysDictData);
        // 排序
        forecastModelList.sort(Comparator.comparing(SysDictData::getDictSort));
        map.put("forecastModelList",forecastModelList);

        return R.ok(map);
    }
}
