package com.jiayue.ipp.idp.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiayue.ipp.common.data.entity.ElectricField;
import com.jiayue.ipp.common.data.entity.ForecastManufactor;
import com.jiayue.ipp.idp.service.ElectricFieldService;
import com.jiayue.ipp.idp.service.ForecastManufactorService;
import com.jiayue.ipp.idp.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.*;


/**
 * idp_forecast_manufactor
 *
 * @author whc
 * @date 2022-03-29 15:24:47
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/forecastmanufactor")
@Api(value = "forecastmanufactor", tags = "idp_forecast_manufactor管理")
public class ForecastManufactorController {

    private final ForecastManufactorService forecastManufactorService;

    private final ElectricFieldService electricFieldService;

    /**
     * 分页查询
     *
     * @param page               分页对象
     * @param forecastManufactor idp_forecast_manufactor
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    //@PreAuthorize("@pms.hasPermission('idp_forecastmanufactor_get')")
    public R getForecastManufactorPage(Page page, ForecastManufactor forecastManufactor) {
        return R.ok(forecastManufactorService.page(page, Wrappers.query(forecastManufactor)));
    }

    /**
     * 以key value格式给选择框提供查询所有预测厂家
     *
     * @return
     */
    @GetMapping("/all")
    public R findAll() {
        List<ForecastManufactor> forecastManufactorList = forecastManufactorService.list();
        List<Map<String, String>> list = new ArrayList<>();
        for (ForecastManufactor f : forecastManufactorList) {
            Map<String, String> map = new HashMap<>();
            map.put("label", f.getName());
            map.put("value", f.getSign());
            list.add(map);
        }
        return R.ok(list);
    }

    /**
     * 通过id查询idp_forecast_manufactor
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}")
    //@PreAuthorize("@pms.hasPermission('idp_forecastmanufactor_get')")
    public R getById(@PathVariable("id") String id) {
        return R.ok(forecastManufactorService.getById(id));
    }

    /**
     * 新增idp_forecast_manufactor
     *
     * @param forecastManufactor idp_forecast_manufactor
     * @return R
     */
    @ApiOperation(value = "新增idp_forecast_manufactor", notes = "新增idp_forecast_manufactor")
    //    //    @SysLog("新增idp_forecast_manufactor")
    @PostMapping
    //@PreAuthorize("@pms.hasPermission('idp_forecastmanufactor_add')")
    public R save(@RequestBody ForecastManufactor forecastManufactor) {
        return R.ok(forecastManufactorService.save(forecastManufactor));
    }

    /**
     * 修改idp_forecast_manufactor
     *
     * @param forecastManufactor idp_forecast_manufactor
     * @return R
     */
    @ApiOperation(value = "修改idp_forecast_manufactor", notes = "修改idp_forecast_manufactor")
    //    //    @SysLog("修改idp_forecast_manufactor")
    @PutMapping
    //@PreAuthorize("@pms.hasPermission('idp_forecastmanufactor_edit')")
    public R updateById(@RequestBody ForecastManufactor forecastManufactor) {
        return R.ok(forecastManufactorService.updateById(forecastManufactor));
    }

    /**
     * 通过id删除idp_forecast_manufactor
     * 如果场站或者上报对象配置了该厂家 则无法删除
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除idp_forecast_manufactor", notes = "通过id删除idp_forecast_manufactor")
    //    //    @SysLog("通过id删除idp_forecast_manufactor")
    @DeleteMapping("/{id}")
    //@PreAuthorize("@pms.hasPermission('idp_forecastmanufactor_del')")
    public R removeById(@PathVariable String id) {
        ForecastManufactor forecastManufactor = forecastManufactorService.getById(id);
        String number = forecastManufactor.getNumber();
        List<ElectricField> electricFieldList = electricFieldService.list();
        //所有场站的预测厂家
        List<String> stringList = new ArrayList<>();
        for (ElectricField e : electricFieldList) {
            if (e.getForecastManufactor() != null) {
                String[] strArr = e.getForecastManufactor().split(",");
                stringList.addAll(Arrays.asList(strArr));
            }
        }
        if (stringList.contains(number)) {
            return R.failed("场站或者上报对象里配置了该厂家,无法删除");
        } else {
            return R.ok(forecastManufactorService.removeById(id));
        }
    }
    /**
     * 以key value格式给选择框提供查询所有预测厂家
     *
     * @return
     */
    @GetMapping("/allList")
    public R getAll() {
        List<ForecastManufactor> forecastManufactorList = forecastManufactorService.list();
        List<Map<String, String>> list = new ArrayList<>();
        for (ForecastManufactor f : forecastManufactorList) {
            Map<String, String> map = new HashMap<>();
            map.put("label", f.getName());
            map.put("value", f.getSign());
            list.add(map);
        }
        return R.ok(list);
    }
    /**
     * 以key value格式给选择框提供查询所有预测厂家
     *
     * @return
     */
    @GetMapping("/allListById")
    public R getAllById() {
        List<ForecastManufactor> forecastManufactorList = forecastManufactorService.list();
        List<Map<String, String>> list = new ArrayList<>();
        for (ForecastManufactor f : forecastManufactorList) {
            Map<String, String> map = new HashMap<>();
            map.put("label", f.getName());
            map.put("value", f.getId());
            list.add(map);
        }
        return R.ok(list);
    }

    @GetMapping("/manufactorStationCode/{stationCode}")
    public R manufactorStationCode(@PathVariable String stationCode) {
        List<Map<String, String>> list = forecastManufactorService.manufactorByStationCode(stationCode);
        return R.ok(list);
    }


    @ApiOperation(value = "提供选择器", notes = "提供选择器")
    @GetMapping("/select")
    public R getSelect() {
        return R.ok(forecastManufactorService.getForecastManufactorSelect());
    }

}
