package com.njcn.gather.aireport.clientunit.controller;

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.response.HttpResult;
import com.njcn.gather.aireport.clientunit.pojo.param.ClientUnitParam;
import com.njcn.gather.aireport.clientunit.pojo.vo.ClientUnitImportResultVO;
import com.njcn.gather.aireport.clientunit.pojo.vo.ClientUnitVO;
import com.njcn.gather.aireport.clientunit.service.IClientUnitService;
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.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 委托单位管理接口。
 */
@Api(tags = "委托单位管理")
@RestController
@RequestMapping("/clientUnit")
@RequiredArgsConstructor
public class ClientUnitController extends BaseController {

    private final IClientUnitService clientUnitService;

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("查询委托单位列表")
    @PostMapping("/list")
    public HttpResult<Page<ClientUnitVO>> list(@RequestBody(required = false) ClientUnitParam.QueryParam param) {
        String methodDescribe = getMethodDescribe("list");
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,
                clientUnitService.listClientUnits(param), methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.ADD)
    @ApiOperation("新增委托单位")
    @PostMapping("/add")
    public HttpResult<Boolean> add(@RequestBody @Validated ClientUnitParam.AddParam param) {
        String methodDescribe = getMethodDescribe("add");
        boolean result = clientUnitService.addClientUnit(param);
        return HttpResultUtil.assembleCommonResponseResult(result ? CommonResponseEnum.SUCCESS : CommonResponseEnum.FAIL,
                result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.UPDATE)
    @ApiOperation("编辑委托单位")
    @PostMapping("/update")
    public HttpResult<Boolean> update(@RequestBody @Validated ClientUnitParam.UpdateParam param) {
        String methodDescribe = getMethodDescribe("update");
        boolean result = clientUnitService.updateClientUnit(param);
        return HttpResultUtil.assembleCommonResponseResult(result ? CommonResponseEnum.SUCCESS : CommonResponseEnum.FAIL,
                result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("查询委托单位详情")
    @ApiImplicitParam(name = "id", value = "委托单位ID", required = true)
    @GetMapping("/getById")
    public HttpResult<ClientUnitVO> getById(@RequestParam("id") String id) {
        String methodDescribe = getMethodDescribe("getById");
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,
                clientUnitService.getClientUnit(id), methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.DELETE)
    @ApiOperation("删除委托单位")
    @PostMapping("/delete")
    public HttpResult<Boolean> delete(@RequestBody List<String> ids) {
        String methodDescribe = getMethodDescribe("delete");
        boolean result = clientUnitService.deleteClientUnits(ids);
        return HttpResultUtil.assembleCommonResponseResult(result ? CommonResponseEnum.SUCCESS : CommonResponseEnum.FAIL,
                result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("导出委托单位")
    @PostMapping("/export")
    public void export(@RequestBody(required = false) ClientUnitParam.QueryParam param) {
        clientUnitService.exportClientUnits(param);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("下载委托单位导入模板")
    @GetMapping("/importTemplate")
    public void importTemplate() {
        clientUnitService.downloadImportTemplate();
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.ADD)
    @ApiOperation("导入委托单位")
    @PostMapping(value = "/import", consumes = {"multipart/form-data"})
    public HttpResult<ClientUnitImportResultVO> importExcel(@RequestParam("file") MultipartFile file) {
        String methodDescribe = getMethodDescribe("importExcel");
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,
                clientUnitService.importClientUnits(file), methodDescribe);
    }
}
