package com.ho.business.controller;

import com.ho.business.entity.PlanCurveOperationRecordReq;
import com.ho.business.entity.PlanningIssueDevice;
import com.ho.business.service.PlanningCurveIssueService;
import com.ho.business.vo.resp.planningCurve.PlanningIssueQueryVo;
import com.ho.business.vo.resp.planningCurve.PlanningIssueVo;
import com.ho.common.tools.annotation.HzPermission;
import com.ho.common.tools.annotation.LogAnnotation;
import com.ho.common.tools.annotation.TokenIgnore;
import com.ho.common.tools.constant.CommonConstant;
import com.ho.common.tools.constant.ContextConstant;
import com.ho.common.tools.constant.PermissionConstant;
import com.ho.common.tools.exception.BaseResponseCode;
import com.ho.common.tools.exception.BusinessException;
import com.ho.common.tools.exception.DataResult;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(ContextConstant.BUSINESS + "planningIssue")
@Api(tags = "计划曲线下发")
@Slf4j
public class PlanningCurveIssueController {

    @Autowired
    private PlanningCurveIssueService issueService;

    @PostMapping("getIssueDevices")
    @ApiOperation(value = "查询下发设备接口")
    @TokenIgnore
    public DataResult getIssueDevices(@RequestBody PlanningIssueDevice issueDevice, HttpServletRequest request) {
        issueDevice.setLang(request.getHeader(CommonConstant.LANG));
        return DataResult.success(issueService.getIssueDevices(issueDevice));
    }

    @PostMapping("getTemplate")
    @ApiOperation(value = "查询计划曲线模板接口")
    @TokenIgnore
    public DataResult getTemplate(@RequestBody PlanningIssueDevice issueDevice,HttpServletRequest request) {
        issueDevice.setLang(request.getHeader(CommonConstant.LANG));
        return DataResult.success(issueService.getTemplate(issueDevice));
    }

    @PostMapping("getIssueDatas")
    @ApiOperation(value = "查询下发模型数据")
    @HzPermission(PermissionConstant.STRATEGY_PLANCURVEDIS_COMMANDISSUANCE)
    @LogAnnotation(title = "计划曲线下发", action = "命令下发")
    public DataResult getIssueDatas(@RequestBody PlanningIssueVo issue){
        List<Map<String, Object>> issueDatas = issueService.getIssueDatas(issue);
        return DataResult.success(issueDatas);
    }

    @PostMapping("getPlanCurveIssueData")
    @ApiOperation(value = "计划曲线投运时间模型数据")
    @HzPermission(PermissionConstant.STRATEGY_PLANCURVEDIS_COMMANDISSUANCEDATA)
    @LogAnnotation(title = "计划曲线下发", action = "控制下发")
    public DataResult getPlanCurveIssueData(@RequestBody PlanningIssueQueryVo issue){
        List<Map<String, Object>> issueDatas = issueService.getPlanCurveIssueData(issue);
        return DataResult.success(issueDatas);
    }

    @PostMapping("insertIssueStatus")
    @ApiOperation(value = "保存设备下发状态接口")
    @TokenIgnore
    public DataResult insertIssueStatus(@RequestBody PlanningIssueDevice issueDevice) {
        if( issueDevice == null || issueDevice.getPlanTemId() == null ){
            throw new BusinessException(BaseResponseCode.TEM_NOT_EXISTS);
        }
        return DataResult.success(issueService.insertIssueStatus(issueDevice));
    }

    @PostMapping("queryIssueStatus")
    @ApiOperation(value = "查看下发状态")
    @TokenIgnore
    public DataResult queryIssueStatus(@RequestBody PlanningIssueDevice issueDevice) {
        return DataResult.success(issueService.queryIssueStatus(issueDevice));
    }

    @PostMapping("queryCurrentIssue")
    @ApiOperation(value = "查看正在使用的计划曲线")
    @TokenIgnore
    public DataResult queryCurrentIssue(@RequestBody PlanningIssueDevice issue){
        return DataResult.success(issueService.queryCurrentIssue(issue));
    }

    @PostMapping("planCurveOperationRecord")
    @ApiOperation(value = "保存计划曲线下发操作记录")
    @TokenIgnore
    public DataResult planCurveOperationRecord(@RequestBody PlanCurveOperationRecordReq planCurveOperationRecordReq){
        if(planCurveOperationRecordReq.getLatestPlanningCurve() != null){
            issueService.planCurveOperationRecord(planCurveOperationRecordReq);
        }
        return DataResult.success();
    }

    @PostMapping("getPlanCurveOperationList")
    @ApiOperation(value = "查询计划曲线下发操作记录")
    @TokenIgnore
    public DataResult<List<PlanCurveOperationRecordReq>> getPlanCurveOperationList(@RequestBody String stationId,HttpServletRequest request){
        String lang = request.getHeader(CommonConstant.LANG);
        List<PlanCurveOperationRecordReq> result = issueService.getPlanCurveOperationList(stationId,lang);
        return DataResult.success(result);
    }

    @PostMapping("getPlanningCurveHistory")
    @ApiOperation(value = "查询操作记录对应计划曲线")
    @TokenIgnore
    public DataResult<Map<String,Object>> getPlanningCurveHistory(@RequestBody List<String> planningCurveIds){
        Map<String,Object> result = issueService.getPlanningCurveHistory(planningCurveIds);
        return DataResult.success(result);
    }

}
