package com.energy.manage.service.controller.powerwordcontract;


import com.energy.manage.common.base.Page;
import com.energy.manage.common.enums.IdPrefixEnum;
import com.energy.manage.common.reponse.ResultResp;
import com.energy.manage.service.config.annotations.UserLoginToken;
import com.energy.manage.service.controller.base.BaseServiceController;
import com.energy.manage.service.domain.dto.WindFieldContract.WindFieldContractDelDto;
import com.energy.manage.service.domain.dto.powerwordcontract.PowerWordContractDelDto;
import com.energy.manage.service.domain.dto.powerwordcontract.PowerWordContractDto;
import com.energy.manage.service.domain.dto.powerwordcontract.PowerWordContractExcelDto;
import com.energy.manage.service.domain.dto.powerwordcontract.PowerWordContractPageDto;
import com.energy.manage.service.domain.vo.WindFieldContract.WindFieldContractPageVo;
import com.energy.manage.service.domain.vo.powerwordcontract.PowerWordContractPageVo;
import com.energy.manage.service.domain.vo.windfield.WindFieldPageVo;
import com.energy.manage.service.service.powerwordcontract.PowerWordContractService;
import com.energy.manage.service.service.windfield.WindFieldContractService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Slf4j
@RestController
@CrossOrigin
@Api(value = "PowerWordContractController", tags = "合同功率曲线接口")
@RequestMapping(value = "/api/powerword/contract")
public class PowerWordContractController extends BaseServiceController {

    @Autowired
    private PowerWordContractService powerWordContractService;

    @Autowired
    private WindFieldContractService windFieldContractService;


    @UserLoginToken
    @PostMapping(value = "/powerWordContractImportData")
    @ApiOperation(value = " 同步合同功率,（选择风场编号，导入功率曲线）")
    public ResultResp powerWordContractImportData(@RequestPart("file") MultipartFile file,
                                                  @RequestParam("fieldCode") String fieldCode,
                                                  @RequestParam("millTypeCode") String millTypeCode,
                                                  @RequestParam("machineTypeCode") String machineTypeCode) {
        if(!fieldCode.contains(IdPrefixEnum.WIND_FIELD_NUMBER.getCode())){
            return ResultResp.FAIL("风场编号不正确，请确认数据！");
        }
        PowerWordContractExcelDto powerWordContractExcelDto = new PowerWordContractExcelDto();
        powerWordContractExcelDto.setFieldCode(fieldCode);
        powerWordContractExcelDto.setMillTypeCode(millTypeCode);
        powerWordContractExcelDto.setCreateBy(getUserId());
        powerWordContractExcelDto.setMachineTypeCode(machineTypeCode);
        try {
            return powerWordContractService.powerWordContractImportData(file, powerWordContractExcelDto);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ResultResp.FAIL();
    }


    @UserLoginToken
    @PostMapping(value = "/powerWordContractPage")
    @ApiOperation(value = " 同步合同功率曲线分页查询")
    public ResultResp<Page<PowerWordContractPageVo>> powerWordContractPage(@RequestBody PowerWordContractPageDto powerWordContractPageDto) {

        Page<PowerWordContractPageVo> page = powerWordContractService.powerWordContractPage(powerWordContractPageDto);
        return ResultResp.SUCCESS(page);
    }


    @Deprecated
    @UserLoginToken
    @PostMapping(value = "/delPowerWordContract")
    @ApiOperation(value = "删除风机合同功率曲线")
    public ResultResp delPowerWordContract(@RequestBody PowerWordContractDelDto powerWordContractDelDto) {

        boolean flg = powerWordContractService.delPowerWordContract(powerWordContractDelDto);
        return flg ? ResultResp.SUCCESS() : ResultResp.FAIL();
    }


    @UserLoginToken
    @PostMapping(value = "/updatePowerWordContract")
    @ApiOperation(value = "编辑合同功率曲线")
    public ResultResp updatePowerWordContract(@RequestBody PowerWordContractDto powerWordContractDto) {
        boolean flg = powerWordContractService.updatePowerWordContract(powerWordContractDto);
        return flg ? ResultResp.SUCCESS() : ResultResp.FAIL();
    }

    @UserLoginToken
    @PostMapping(value = "/getWindFieldContractByFieldCode")
    @ApiOperation(value = "通过风场编号查询合同功率曲线集合")
    public ResultResp<List<WindFieldContractPageVo>> getWindFieldContractByFieldCode(@RequestBody PowerWordContractPageDto dto) {
        List<WindFieldContractPageVo> list = windFieldContractService.getWindFieldContract(dto.getFieldCode());
        return ResultResp.SUCCESS(list);
    }


    @UserLoginToken
    @PostMapping(value = "/delWindFieldContractById")
    @ApiOperation(value = "删除合同功率曲线通过id")
    public ResultResp delWindFieldContractById(@RequestBody WindFieldContractDelDto windFieldContractDelDto) {
        boolean flg  = windFieldContractService.delWindFieldContract(windFieldContractDelDto);
        return flg ? ResultResp.SUCCESS() : ResultResp.FAIL();
    }


}
