package com.njcn.product.cnzutai.zutai.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
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.minioss.bo.MinIoUploadResDTO;
import com.njcn.product.cnzutai.zutai.pojo.param.CsConfigurationParm;
import com.njcn.product.cnzutai.zutai.pojo.po.CsConfigurationPO;
import com.njcn.product.cnzutai.zutai.pojo.vo.CsConfigurationVO;
import com.njcn.product.cnzutai.zutai.service.CsConfigurationService;
import com.njcn.web.controller.BaseController;
import com.njcn.web.utils.HttpResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Objects;

/**
* (cs_configuration)表控制层
*
* @author xxxxx
*/
@Slf4j
@RestController
@RequestMapping("/cs-harmonic-boot/csconfiguration")
@Api(tags = "组态项目")
@AllArgsConstructor
public class CsConfigurationController extends BaseController {

    private final CsConfigurationService csConfigurationService;

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/add")
    @ApiOperation("新增组态项目")
    @ApiImplicitParam(name = "csConfigurationParm", value = "新增组态项目参数", required = true)
    public HttpResult<Boolean> add(@RequestBody @Validated CsConfigurationParm csConfigurationParm){
        String methodDescribe = getMethodDescribe("add");

        boolean save = csConfigurationService.add(csConfigurationParm);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, save, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/audit")
    @ApiOperation("修改组态项目")
    @ApiImplicitParam(name = "auditParm", value = "修改组态项目参数", required = true)
    public HttpResult<Boolean> audit(@RequestBody @Validated CsConfigurationParm.CsConfigurationAuditParam auditParm){
        String methodDescribe = getMethodDescribe("audit");

        boolean save = csConfigurationService.audit (auditParm);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, save, methodDescribe);
    }


    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @GetMapping("/active")
    @ApiOperation("激活组态项目")
    public HttpResult<Boolean> active(@RequestParam("id")String id){
        String methodDescribe = getMethodDescribe("active");
        boolean active = csConfigurationService.active(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, active, methodDescribe);
    }


    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @GetMapping("/getActive")
    @ApiOperation("获取激活组态项目")
    public HttpResult<CsConfigurationPO> getActive(){
        String methodDescribe = getMethodDescribe("active");
        CsConfigurationPO active = csConfigurationService.getActive();
        if(Objects.nonNull(active)){
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, active, methodDescribe);
        }else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }
    }


    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/queryPage")
    @ApiOperation("组态项目分页查询")
    @ApiImplicitParam(name = "csConfigurationQueryParam", value = "组态项目查询参数", required = true)
    public HttpResult<IPage<CsConfigurationVO>> queryPage(@RequestBody @Validated CsConfigurationParm.CsConfigurationQueryParam csConfigurationQueryParam ){
        String methodDescribe = getMethodDescribe("queryPage");

        IPage<CsConfigurationVO> page  = csConfigurationService.queryPage (csConfigurationQueryParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, page, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.UPLOAD)
    @PostMapping("/uploadImage")
    @ApiOperation("上传底图")
    @ApiImplicitParam(name = "file", value = "底图文件", required = true)
    public HttpResult<MinIoUploadResDTO> uploadImage(@RequestParam("file") MultipartFile issuesFile){
        String methodDescribe = getMethodDescribe("uploadImage");
        String filePath = csConfigurationService.uploadImage(issuesFile);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,new MinIoUploadResDTO(issuesFile.getOriginalFilename(),filePath), methodDescribe);
    }

}
