package com.njcn.device.device.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njcn.common.pojo.annotation.OperateInfo;
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.device.device.service.ProgramVersionService;
import com.njcn.device.pq.pojo.param.ProgramParam;
import com.njcn.device.pq.pojo.vo.VersionVO;
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.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDate;
import java.util.List;

/**
 * @version 1.0.0
 * @author: chenchao
 * @date: 2022/05/19 14:44
 */
@Validated
@Slf4j
@RestController
@RequestMapping("/programVersion")
@Api(tags = "终端版本维护")
@AllArgsConstructor
public class ProgramVersionController extends BaseController {

    private final ProgramVersionService programService;

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getProgramVersion")
    @ApiOperation("程序版本查询")
    @ApiImplicitParam(name = "programParam", value = "程序版本查询", required = true)
    public HttpResult<Page<VersionVO>> getProgramVersion(@RequestBody @Validated ProgramParam programParam) {
        String methodDescribe = getMethodDescribe("getProgramVersion");
        Page<VersionVO> list = programService.getProgramVersion(programParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ResponseBody
    @PostMapping("/addProgramVersion")
    @ApiOperation("新增程序版本")
    public HttpResult addProgramVersion(@RequestParam("name") String name,
                                        @RequestParam("protocol") String protocol,
                                        @DateTimeFormat(pattern="yyyy-MM-dd") @RequestParam("date") LocalDate date,
                                        @RequestParam("devType") String devType,
                                        @RequestParam("remark") String remark,
                                        @RequestParam(value = "versionType",required = false) String versionType,
                                        @RequestParam("filename") String fileName,
                                        @ApiParam(value = "文件") @RequestPart(value = "file",required = false) MultipartFile file) {
        String methodDescribe = getMethodDescribe("addProgramVersion");
        boolean res = programService.addProgramVersion(name,protocol,date,devType,remark,versionType,fileName,file);
        if (res) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        } else {
            throw new BusinessException(CommonResponseEnum.FAIL);
        }
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getProgramVersionById")
    @ApiOperation("版本文件是否存在")
    @ApiImplicitParam(name = "id", value = "终端版本id", required = true)
    public HttpResult getProgramVersionById(@RequestParam("id") String id) {
        String methodDescribe = getMethodDescribe("getProgramVersionById");
        boolean res = programService.getProgramVersionById(id);
        if (res) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FILE_EXIST, null, methodDescribe);
        } else {
            throw new BusinessException(CommonResponseEnum.NO_DATA);
        }
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/updateProgramVersion")
    @ApiOperation("修改程序版本")
    public HttpResult updateProgramVersion(@RequestParam("id") String id,
                                           @RequestParam("name") String name,
                                           @RequestParam("protocol") String protocol,
                                           @DateTimeFormat(pattern="yyyy-MM-dd") @RequestParam("date") LocalDate date,
                                           @RequestParam("devType") String devType,
                                           @RequestParam("remark") String remark,
                                           @RequestParam(value = "versionType",required = false) String versionType,
                                           @RequestParam("filename") String fileName,
                                           @ApiParam(value = "文件") @RequestPart(name = "file",required = false) MultipartFile file) {
        String methodDescribe = getMethodDescribe("updateProgramVersion");
        boolean res = programService.updateProgramVersion(id,name,protocol,date,devType,remark,versionType,fileName,file);
        if (res) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        } else {
            throw new BusinessException(CommonResponseEnum.FAIL);
        }
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/updateVersionFlag")
    @ApiOperation("修改程序版本状态")
    @ApiImplicitParam(name = "id", value = "终端版本id", required = true)
    public HttpResult updateVersionFlag(@RequestParam("id") String id, @RequestParam("versionFlag") Integer flag) {
        String methodDescribe = getMethodDescribe("updateVersionFlag");
        boolean res = programService.updateVersionFlag(id,flag);
        if (res) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        } else {
            throw new BusinessException(CommonResponseEnum.FAIL);
        }
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/delProgramVersion")
    @ApiOperation("删除程序版本")
    @ApiImplicitParam(name = "id", value = "终端版本id", required = true)
    public HttpResult delProgramVersion(@RequestParam("id") String id) {
        String methodDescribe = getMethodDescribe("delProgramVersion");
        boolean res = programService.delProgramVersion(id);
        if (res) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        } else {
            throw new BusinessException(CommonResponseEnum.FAIL);
        }
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getVersion")
    @ApiOperation("查看除本外其他版本信息")
    @ApiImplicitParam(name = "param", value = "程序版本查询", required = true)
    public HttpResult<List<VersionVO>> getVersion(@RequestBody @Validated ProgramParam.Version param) {
        String methodDescribe = getMethodDescribe("getVersion");
        List<VersionVO> version = programService.getVersion(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, version, methodDescribe);
    }

}
