package com.artfess.cqxy.completionAcceptance.controller;

import com.artfess.base.annotation.ApiGroup;
import com.artfess.base.constants.ApiGroupConsts;
import com.artfess.base.controller.BaseController;
import com.artfess.base.model.CommonResult;
import com.artfess.base.query.PageList;
import com.artfess.base.query.QueryFilter;
import com.artfess.base.util.BeanUtils;
import com.artfess.cqxy.completionAcceptance.model.AcceptanceSettlement;
import com.artfess.cqxy.completionAcceptance.manager.AcceptanceSettlementManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;
import java.util.Arrays;
import java.util.List;

/**
 * 竣工验收 - 结算表(BizAcceptanceSettlement)表控制层
 *
 * @author 黎沐华
 * @since 2022-03-07 15:15:05
 */
@Slf4j
@RestController
@Api(tags = "竣工验收-结算接口")
@ApiGroup(group = {ApiGroupConsts.GROUP_BIZ})
@RequestMapping("/biz/completionAcceptance/acceptanceSettlement/v1")
public class AcceptanceSettlementController extends BaseController<AcceptanceSettlementManager, AcceptanceSettlement> {

    /**
     * 保存或修改数据
     * @param acceptanceSettlement 实体对象
     * @return 新增或更新结果
     */
    @PostMapping("/save")
    @ApiOperation(value = "S-保存或修改数据",httpMethod = "POST")
    public CommonResult<String> insert(@RequestBody AcceptanceSettlement acceptanceSettlement) {
        log.debug("结算-保存或修改数据接口请求参数：{}", acceptanceSettlement);
        Assert.notNull(acceptanceSettlement.getProjectId(),"项目ID不能为空");
        Assert.notNull(acceptanceSettlement.getContractId(),"合同ID不能为空");
        boolean add = StringUtils.isEmpty(acceptanceSettlement.getId());
        boolean save = baseService.saveOrUpdate(acceptanceSettlement);
        String msg = (add ? "添加" : "修改")+(save ? "成功" : "失败");
        log.debug("结算-保存或修改数据接口响应结果：{}", msg);
        return new CommonResult<>(save, msg);
    }

    /**
     * 批量导入Excel数据
     * @param file Excel文件
     */
    @PostMapping("/importExcelData")
    @ApiOperation(value = "S-批量导入Excel数据")
    public CommonResult importExcelData (
            @ApiParam("上传文件") @RequestParam("file") MultipartFile file,
            @ApiParam("项目ID") @RequestParam("projectId") String projectId,
            @ApiParam("合同ID") @RequestParam("contractId") String contractId,
            @ApiParam("合同名称") @RequestParam("contractName") String contractName){
        log.debug("批量导入Excel数据接口请求参数：{}",file, projectId,contractId,contractName);
        baseService.importExcelData(file, projectId,contractId,contractName);
        return new CommonResult<>("导入成功");
    }

    /**
     * 导出数据到Excel
     * @param queryFilter 通用查询器
     * @param response 响应对象
     * @return 通用结果
     * @throws IOException IO异常
     */
    @PostMapping("/exportDataToExcel")
    @ApiOperation(value = "S-导出数据到Excel",notes = "参数说明：查询器只使到了query组传参，其他都可以不要，查询条件要带上表别名：主表-结算信息 bas、副表-项目信息 pm、副表 合同信息 bc；传入pm.ID_导出该项目下的数据，否则将导出所有数据")
    public CommonResult exportDataToExcel (@ApiParam(name = "queryFilter",value = "通用查询器")@RequestBody QueryFilter<AcceptanceSettlement> queryFilter, HttpServletResponse response) throws IOException {
        log.debug("导出数据到Excel接口请求参数：{}",queryFilter,response);
        baseService.exportDataToExcel(queryFilter,response);
        return new CommonResult<>("导出成功");
    }

    /**
     * 删除数据
     * @param ids 主键集合
     * @return 删除结果
     */
    @PostMapping("/delete")
    @ApiOperation(value = "S-删除数据",httpMethod = "POST")
    public CommonResult delete(@ApiParam(name = "ids",value = "ID集合") @RequestParam("ids") String ids) {
        log.debug("结算-删除数据接口请求参数：{}", ids);
        Assert.notNull(ids,"主键集合不能为空");
        boolean state = baseService.deleteByIds(Arrays.asList(ids.split(",")));
        String msg = state ? "删除成功" : "删除失败";
        log.debug("结算-删除数据接口响应结果：{}", msg);
        return new CommonResult<String>(state, msg);
    }

    /**
     * 通过主键查询单条数据
     * @param id 主键
     * @return 单条数据
     */
    @PostMapping("/detail/{id}")
    @ApiOperation(value = "S-根据ID查询详情",httpMethod = "POST")
    public CommonResult<AcceptanceSettlement> selectOne(@ApiParam(name = "id",value = "主键ID") @PathVariable String id) {
        log.debug("结算-根据ID查询详情 接口请求参数：{}", id);
        Assert.notNull(id,"主键不能为空");
        AcceptanceSettlement result = baseService.getById(id);
        boolean state = !BeanUtils.isEmpty(result);
        log.debug("结算-根据ID查询详情 接口响应结果：{}", result);
        return new CommonResult<AcceptanceSettlement>(state, state?"操作成功":"该ID没有对应数据", result);
    }

    /**
     * 分页查询所有数据
     * @param queryFilter 通用查询器
     * @return 分页数据
     */
    @PostMapping("/page")
    @ApiOperation(value = "S-分页查询所有数据",notes = "参数说明：在通用查询器中的query对象组传入查询参数，查询条件要带上表别名：主表-结算信息 bas、副表-项目信息 pm、副表 合同信息 bc；传入pm.ID_ 项目ID 以切换项目",httpMethod = "POST")
    public CommonResult<PageList<AcceptanceSettlement>> selectAll(@ApiParam(name = "queryFilter",value = "通用查询器")@RequestBody QueryFilter<AcceptanceSettlement> queryFilter) {
        log.debug("结算-分页查询所有数据接口请求参数：{}", queryFilter);
        PageList<AcceptanceSettlement> result = baseService.queryAllByPage(queryFilter);
        log.debug("结算-分页查询所有数据接口响应结果：{}", result);
        return new CommonResult<>(true,"操作成功",result);
    }

}

