package com.njcn.harmonic.controller.upload;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.exception.BusinessException;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.HttpResultUtil;
import com.njcn.harmonic.pojo.po.upload.RUploadReport;
import com.njcn.harmonic.service.upload.IRUploadReportService;
import com.njcn.user.api.DeptFeignClient;
import com.njcn.user.pojo.po.Dept;
import com.njcn.web.enums.GWSendEnum;
import com.njcn.web.factory.PageFactory;
import com.njcn.web.pojo.param.BaseParam;
import com.njcn.web.pojo.param.SendParam;
import com.njcn.web.utils.GwSendUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.njcn.web.controller.BaseController;

import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * <p>
 * 电能质量报表上送 前端控制器
 * </p>
 *
 * @author cdf
 * @since 2024-08-07
 */
@RestController
@RequestMapping("/rUploadReport")
@RequiredArgsConstructor
@Api(tags = "电能质量报表上送")
public class RUploadReportController extends BaseController {

    private final IRUploadReportService irUploadReportService;

    private final DeptFeignClient deptFeignClient;


    /**
     * 新增pms电能质量报表
     */
    @PostMapping("/addPmsReport")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON,operateType = OperateType.ADD)
    @ApiOperation("新增pms电能质量报表")
    @ApiImplicitParam(name = "rUploadReport", value = "条件参数", required = true)
    public HttpResult<Boolean> addPmsReport(@RequestBody @Validated RUploadReport rUploadReport){
        String methodDescribe = getMethodDescribe("addPmsReport");
        Dept dept = deptFeignClient.getRootDept().getData();
        List<Dept> deptList = deptFeignClient.getDirectSonSelf(dept.getId()).getData();
        Optional<Dept> optional = deptList.stream().filter(item->item.getPid().equals(dept.getId())).findFirst();
        if(optional.isPresent()){
            Dept d = optional.get();
            rUploadReport.setProvinceOrg(d.getCode());
            rUploadReport.setProvinceOrgName(d.getName());
        }
        rUploadReport.setIsDelete("0");
        rUploadReport.setUploadStatus(0);

        long count = irUploadReportService.count(new LambdaQueryWrapper<RUploadReport>().eq(RUploadReport::getProvinceOrg,rUploadReport.getProvinceOrg()));
        if(count>0){
            throw new BusinessException("已经存在该省电能质量报告");
        }
        irUploadReportService.save(rUploadReport);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }


    /**
     * 修改pms电能质量报表
     */
    @PostMapping("/updatePmsReport")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON,operateType = OperateType.UPLOAD)
    @ApiOperation("修改pms电能质量报表")
    @ApiImplicitParam(name = "rUploadReport", value = "条件参数", required = true)
    public HttpResult<Boolean> updatePmsReport(@RequestBody @Validated RUploadReport rUploadReport){
        String methodDescribe = getMethodDescribe("updatePmsReport");
        irUploadReportService.updateById(rUploadReport);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }


    /**
     * 删除pms电能质量报表
     */
    @PostMapping("/delPmsReport")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON,operateType = OperateType.DELETE)
    @ApiOperation("删除pms电能质量报表")
    public HttpResult<Boolean> delPmsReport(@RequestBody List<String> ids){
        String methodDescribe = getMethodDescribe("delPmsReport");
        irUploadReportService.removeByIds(ids);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }


    @PostMapping("/pagePmsReportList")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("列表集合")
    public HttpResult<Page<RUploadReport>> pagePmsReportList(@RequestBody BaseParam baseParam){
        String methodDescribe = getMethodDescribe("pagePmsReportList");
        Page<RUploadReport> reportList = irUploadReportService.page(new Page<>(PageFactory.getPageNum(baseParam),PageFactory.getPageSize(baseParam)));
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, reportList, methodDescribe);
    }

    @PostMapping("/uploadGw")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("上送电能质量报表数据新增或更新")
    public HttpResult<Integer> uploadGw(@RequestBody List<String> ids){
        String methodDescribe = getMethodDescribe("uploadGw");
        List<RUploadReport> reportList = irUploadReportService.listByIds(ids);
        SendParam sendParam = new SendParam();
        sendParam.setStats(reportList);
        Map<String,String> map = GwSendUtil.send(sendParam, GWSendEnum.REPORT_CREATE);
        int result = GwSendUtil.returnInfoMsg(ids,map);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }


}

