package com.njcn.supervision.controller.user;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njcn.bpm.pojo.param.instance.BpmProcessInstanceCancelParam;
import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.constant.OperateType;
import com.njcn.common.pojo.enums.common.LogEnum;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.HttpResultUtil;
import com.njcn.supervision.pojo.param.SensitiveUserParam;
import com.njcn.supervision.pojo.param.user.UserReportParam;
import com.njcn.supervision.pojo.po.user.UserReportPO;
import com.njcn.supervision.pojo.vo.user.NewUserReportVO;
import com.njcn.supervision.pojo.vo.user.UserLedgerVO;
import com.njcn.supervision.pojo.vo.user.UserReportVO;
import com.njcn.supervision.service.user.IUserReportNormalService;
import com.njcn.supervision.service.user.UserReportPOService;
import com.njcn.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 干扰源用户管理
 *
 * @author qijian
 * @version 1.0.0
 * @createTime 2022/11/11 - 9:20
 */
@Slf4j
@RestController
@RequestMapping("/userReport")
@Api(tags = "干扰源用户管理")
@AllArgsConstructor
public class UserReportManageController extends BaseController {

    private final UserReportPOService userReportPOService;

    private final IUserReportNormalService userReportNormalService;

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.ADD)
    @PostMapping("/add")
    @ApiOperation("新增干扰源用户")
    @ApiImplicitParam(name = "userReportParam", value = "实体参数", required = true)
    public HttpResult<String> addUserReport(@RequestBody @Validated UserReportParam userReportParam) {
        String methodDescribe = getMethodDescribe("addUserReport");
        String userReportId = userReportPOService.addUserReport(userReportParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, userReportId, methodDescribe);
    }

    @PostMapping("/update")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.UPDATE)
    @ApiOperation("重新发起")
    @ApiImplicitParam(name = "userReportParam", value = "实体参数", required = true)
    public HttpResult<Object> updateUserReport(@RequestBody @Validated UserReportParam.UserReportUpdate userReportParam) {
        String methodDescribe = getMethodDescribe("updateUserReport");
        String userReportId = userReportPOService.updateUserReport(userReportParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, userReportId, methodDescribe);
    }


    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.ADD)
    @PostMapping("/auditUserReport")
    @ApiOperation("修改干扰源用户")
    @ApiImplicitParam(name = "userReportUpdate", value = "实体参数", required = true)
    public HttpResult<Boolean> auditUserReport(@RequestBody @Validated UserReportParam.UserReportUpdate userReportUpdate) {
        String methodDescribe = getMethodDescribe("auditUserReport");
        boolean res = userReportPOService.auditUserReport(userReportUpdate);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, res, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getUserReport")
    @ApiOperation("分页查询当前用户能看到的用户建档数据")
    @ApiImplicitParam(name = "userReportQueryParam", value = "参数", required = true)
    public HttpResult<Page<UserReportVO>> getUserReport(@RequestBody @Validated UserReportParam.UserReportQueryParam userReportQueryParam) {
        String methodDescribe = getMethodDescribe("getUserReport");
        Page<UserReportVO> out = userReportPOService.getUserReport(userReportQueryParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, out, methodDescribe);
    }

    @GetMapping("/getUserReportById")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("根据id获取用户档案录入数据")
    public HttpResult<UserReportVO> getUserReportById(String id) {
        String methodDescribe = getMethodDescribe("getUserReportById");
        UserReportVO userReportVO = userReportPOService.getUserReportById(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, userReportVO, methodDescribe);
    }


    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/removeUserReport")
    @ApiOperation("删除干扰源用户记录")
    public HttpResult<Boolean> removeUserReport(@RequestParam("ids") List<String> ids) {
        String methodDescribe = getMethodDescribe("removeUserReport");
        Boolean flag = userReportPOService.removeUserReport(ids);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, flag, methodDescribe);
    }

    @GetMapping("/getById")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("根据id获取用户档案录入的详细数据")
    public HttpResult<UserReportVO.UserReportListVO> getById(String id) {
        String methodDescribe = getMethodDescribe("getById");
        UserReportVO.UserReportListVO userReportVO = userReportPOService.getVOById(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, userReportVO, methodDescribe);
    }

    @GetMapping("/getUserReportByFangAnId")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("根据id获取用户档案录入的详细数据")
    public HttpResult<UserReportVO.UserReportListVO> getUserReportByFangAnId(String id) {
        String methodDescribe = getMethodDescribe("getUserReportByFangAnId");
        UserReportVO.UserReportListVO userReportVO = userReportNormalService.getUserReportByFangAnId(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, userReportVO, methodDescribe);
    }




    @GetMapping("/updateUserReportStatus")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("更新用户数据流程状态")
    public HttpResult<Object> updateUserReportStatus(String businessKey, Integer status) {
        String methodDescribe = getMethodDescribe("updateUserReportStatus");
        userReportPOService.updateProcessStatus(businessKey, status);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    @GetMapping("/updateUserStatus")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("更新干扰源用户状态")
    public HttpResult<Object> updateUserStatus(String lineId, Integer userStatus) {
        String methodDescribe = getMethodDescribe("updateUserStatus");
        userReportPOService.updateUserStatus(lineId, userStatus);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getUserReportList")
    @ApiOperation("查询干扰源下拉列表")
    public HttpResult<List<UserReportVO>> getUserReportList() {
        String methodDescribe = getMethodDescribe("getUserReportList");
        List<UserReportVO> out = userReportPOService.getUserReportList();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, out, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getNormalUserPage")
    @ApiOperation("分页查询常态化干扰源用户台账")
    @ApiImplicitParam(name = "userReportQueryParam", value = "参数", required = true)
    public HttpResult<Page<UserReportVO>> getNormalUserPage(@RequestBody @Validated UserReportParam.UserReportQueryParam userReportQueryParam) {
        String methodDescribe = getMethodDescribe("getNormalUserPage");
        Page<UserReportVO> out = userReportPOService.getInterferenceUserPage(userReportQueryParam, true);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, out, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getInterferenceUserPage")
    @ApiOperation("分页查询干扰源用户台账")
    @ApiImplicitParam(name = "userReportQueryParam", value = "参数", required = true)
    public HttpResult<Page<UserReportVO>> getInterferenceUserPage(@RequestBody @Validated UserReportParam.UserReportQueryParam userReportQueryParam) {
        String methodDescribe = getMethodDescribe("getInterferenceUserPage");
        Page<UserReportVO> out = userReportPOService.getInterferenceUserPage(userReportQueryParam, false);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, out, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getSensitiveUserPage")
    @ApiOperation("分页查询敏感及重要用户台账")
    @ApiImplicitParam(name = "userReportQueryParam", value = "参数", required = true)
    public HttpResult<Page<UserReportVO>> getSensitiveUserPage(@RequestBody @Validated UserReportParam.UserReportQueryParam userReportQueryParam) {
        String methodDescribe = getMethodDescribe("getSensitiveUserPage");
        Page<UserReportVO> out = userReportPOService.getSensitiveUserPage(userReportQueryParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, out, methodDescribe);
    }


    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.ADD)
    @PostMapping("/cancel")
    @ApiOperation("取消")
    @ApiImplicitParam(name = "cancelReqVO", value = "取消原因", required = true)
    public HttpResult<String> cancelUserReport(@Validated @RequestBody BpmProcessInstanceCancelParam cancelReqVO) {
        String methodDescribe = getMethodDescribe("cancelUserReport");
        String userReportId = userReportPOService.cancelUserReport(cancelReqVO);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, userReportId, methodDescribe);
    }


    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @GetMapping(value = "/downloadSensitiveReportTemplate", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    @ApiOperation("下载干扰源用户台账模板")
    public void downloadSensitiveReportTemplate() {
        userReportPOService.downloadSensitiveReportTemplate();
    }


    @PostMapping(value = "/importSensitiveReportData")
    @ApiOperation("批量导入干扰源用户")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.ADD)
    public HttpResult<String> importSensitiveReportData(@ApiParam(value = "文件", required = true) @RequestPart("file") MultipartFile file, HttpServletResponse response) {
        String methodDescribe = getMethodDescribe("importSensitiveReportData");
        userReportPOService.importSensitiveReportData(file, response);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @GetMapping(value = "/downloadSensitiveUserTemplate", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    @ApiOperation("下载敏感及重要用户模板")
    public void downloadSensitiveUserTemplate() {
        userReportPOService.downloadSensitiveUserTemplate();
    }


    @PostMapping(value = "/importSensitiveUserData")
    @ApiOperation("批量导入敏感及重要用户")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.ADD)
    public HttpResult<String> importSensitiveUserData(@ApiParam(value = "文件", required = true) @RequestPart("file") MultipartFile file, HttpServletResponse response) {
        String methodDescribe = getMethodDescribe("importSensitiveUserData");
        userReportPOService.importSensitiveUserData(file, response);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    @PostMapping(value = "/deleteUserReport")
    @ApiOperation("干扰源接入功能删除流程")
    @OperateInfo(info = LogEnum.BUSINESS_MEDIUM, operateType = OperateType.DELETE)
    public HttpResult<Boolean> deleteUserReport(@RequestBody List<String> supervisionId) {
        String methodDescribe = getMethodDescribe("deleteUserReport");
        Boolean b = userReportPOService.deleteUserReport(supervisionId);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, b, methodDescribe);
    }



    @PostMapping(value = "/selectUserList")
    @ApiOperation("查询用户台账")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    public HttpResult<List<UserLedgerVO>> selectUserList(@RequestBody UserReportParam userReportParam) {
        String methodDescribe = getMethodDescribe("selectUserList");
        List<UserLedgerVO> b = userReportPOService.selectUserList(userReportParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, b, methodDescribe);
    }

    @GetMapping(value = "/selectUserInfo")
    @ApiOperation("查询用户基本详情")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    public HttpResult<UserLedgerVO> selectUserInfo(@RequestParam String id) {
        String methodDescribe = getMethodDescribe("selectUserInfo");
        UserLedgerVO b = userReportPOService.selectUserInfo(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, b, methodDescribe);
    }


    @PostMapping(value = "/bindUserStation")
    @ApiOperation("绑定用户电站信息")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    public HttpResult<List<UserLedgerVO>> bindUserStation(@RequestParam("userId")String userId,@RequestParam("stationId")String stationId) {
        String methodDescribe = getMethodDescribe("bindUserStation");
        userReportPOService.bindUserStation(userId,stationId);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }



    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getUserLedgerPage")
    @ApiOperation("分页查询干扰源用户台账")
    @ApiImplicitParam(name = "userReportQueryParam", value = "参数", required = true)
    public HttpResult<Page<UserReportVO>> getUserLedgerPage(@RequestBody @Validated UserReportParam.UserReportQueryParam userReportQueryParam) {
        String methodDescribe = getMethodDescribe("getUserLedgerPage");
        Page<UserReportVO> out = userReportPOService.getUserLedgerPage(userReportQueryParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, out, methodDescribe);
    }

    @PostMapping("/getUserReportByIds")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("根据ids获取非电网侧用户信息")
    public HttpResult<List<NewUserReportVO>> getUserReportByIds(@RequestBody List<String> ids) {
        String methodDescribe = getMethodDescribe("getUserReportByIds");
        List<NewUserReportVO> userReportByIds = userReportNormalService.getUserReportByIds(ids);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, userReportByIds, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getSensitiveUserByDept")
    @ApiOperation("根据部门获取敏感用户信息")
    @ApiImplicitParam(name = "param", value = "参数", required = true)
    public HttpResult<List<UserReportPO>> getSensitiveUserByDept(@RequestBody @Validated SensitiveUserParam param) {
        String methodDescribe = getMethodDescribe("getSensitiveUserByDept");
        List<UserReportPO> list = userReportPOService.getSensitiveUserByDept(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @GetMapping("/bindAssessmentId")
    @ApiOperation("根据用户id绑定二级评估id")
    public HttpResult<Boolean> bindAssessmentId(@RequestParam("userReportId") String userReportId,@RequestParam("assessMentId") String assessMentId) {
        String methodDescribe = getMethodDescribe("bindAssessmentId");
        Boolean b = userReportPOService.bindAssessmentId(userReportId, assessMentId);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, b, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/selectUserAssessMentList")
    @ApiOperation("二级评估查询用户接口")
    public HttpResult<List<UserLedgerVO.AssessMent>> selectUserAssessMentList(@RequestBody UserReportParam userReportParam) {
        String methodDescribe = getMethodDescribe("selectUserAssessMentList");
        List<UserLedgerVO.AssessMent> userReportPOS = userReportPOService.selectUserAssessMentList(userReportParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, userReportPOS, methodDescribe);
    }
}
