package com.njcn.product.advance.responsility.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.dto.SelectOption;
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.product.advance.responsility.pojo.dto.RespDataDTO;
import com.njcn.product.advance.responsility.pojo.param.UserDataIntegrityParam;
import com.njcn.product.advance.responsility.pojo.po.RespUserData;
import com.njcn.product.advance.responsility.pojo.po.RespUserDataIntegrity;
import com.njcn.product.advance.responsility.service.IRespUserDataIntegrityService;
import com.njcn.product.advance.responsility.service.IRespUserDataService;
import com.njcn.web.controller.BaseController;
import com.njcn.web.pojo.param.BaseParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
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 hongawen
 * @version 1.0.0
 * @date 2023年07月13日 14:11
 */
@RestController
@RequestMapping("responsibility")
@Api(tags = "谐波责任划分-用采数据处理")
@RequiredArgsConstructor
public class UserDataController extends BaseController {


    private final IRespUserDataService respUserDataService;

    private final IRespUserDataIntegrityService respUserDataIntegrityService;


    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/userDataList")
    @ApiOperation("查询用户列表分页")
    @ApiImplicitParam(name = "queryParam", value = "查询参数", required = true)
    public HttpResult<Page<RespUserData>> userDataList(@RequestBody @Validated BaseParam queryParam) {
        String methodDescribe = getMethodDescribe("userDataList");
        Page<RespUserData> list = respUserDataService.userDataList(queryParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }


    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/userDataIntegrityList")
    @ApiOperation("用采完整性不足列表")
    @ApiImplicitParam(name = "userDataIntegrityParam", value = "查询参数", required = true)
    public HttpResult<Page<RespUserDataIntegrity>> userDataIntegrityList(@RequestBody @Validated UserDataIntegrityParam userDataIntegrityParam) {
        String methodDescribe = getMethodDescribe("userDataIntegrityList");
        Page<RespUserDataIntegrity> list = respUserDataIntegrityService.userDataIntegrityList(userDataIntegrityParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/deleteUserDataByIds")
    @ApiOperation("删除用采数据")
    @ApiImplicitParam(name = "ids", value = "待删除用采数据id集合", required = true)
    public HttpResult<Page<RespDataDTO>> deleteUserDataByIds(@RequestBody List<String> ids) {
        String methodDescribe = getMethodDescribe("deleteUserDataByIds");
        respUserDataService.deleteUserDataByIds(ids);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }


    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @GetMapping("/userDataSelect")
    @ApiOperation("用采数据下拉")
    public HttpResult<List<SelectOption>> userDataSelect() {
        String methodDescribe = getMethodDescribe("userDataSelect");
        List<SelectOption> listOption = respUserDataService.userDataSelect();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, listOption, methodDescribe);
    }

    /**
     * 上传用采数据，并对用采数据进行数据分析并缓存
     *
     * @param file 上传的表格
     */
    @PostMapping("uploadUserData")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("上传用采数据")
    public HttpResult<Object> uploadUserData(@ApiParam(value = "文件", required = true) @RequestPart("file") MultipartFile file, HttpServletResponse response) {
        String methodDescribe = getMethodDescribe("uploadUserData");
        String fileName = file.getOriginalFilename();
        long fileSize = file.getSize() / 1024;
        //判断文件大小
        if (fileSize > 3072) {
            throw new BusinessException(CommonResponseEnum.FILE_SIZE_ERROR);
        }
        assert fileName != null;
        if (!fileName.matches("^.+\\.(?i)(xlsx)$") && !fileName.matches("^.+\\.(?i)(xls)$")) {
            throw new BusinessException(CommonResponseEnum.FILE_XLSX_ERROR);
        }
        respUserDataService.uploadUserData(file, response);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

}
