package com.casic.missiles.controller.finance;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.casic.missiles.core.base.controller.ExportController;
import com.casic.missiles.core.page.PageFactory;
import com.casic.missiles.core.page.PageInfoBT;
import com.casic.missiles.dto.IdDTO;
import com.casic.missiles.dto.IdsDTO;
import com.casic.missiles.dto.ReturnDTO;
import com.casic.missiles.dto.ReturnUtil;
import com.casic.missiles.dto.finance.SettlementInfoListRequest;
import com.casic.missiles.dto.finance.SettlementInfoListResponse;
import com.casic.missiles.enums.BusinessExceptionEnum;
import com.casic.missiles.enums.ExportEnum;
import com.casic.missiles.exception.BusinessException;
import com.casic.missiles.model.exception.enums.CoreExceptionEnum;
import com.casic.missiles.model.finance.FinanceBusinessSettlementInfo;
import com.casic.missiles.service.finance.IFinanceBusinessSettlementInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 业务结算表-未在合同里的委托书收款信息 前端控制器
 * </p>
 *
 * @author wangpeng
 * @since 2023-03-31
 */
@Api(tags = "财务产值接口-业务结算")
@Slf4j
@RestController
@RequestMapping("/finance/settlement")
public class FinanceBusinessSettlementInfoController extends ExportController {
    @Autowired
    private IFinanceBusinessSettlementInfoService settlementInfoService;

    @ApiOperation("业务结算列表（分页）")
    @PostMapping("/listPage")
    public ReturnDTO<PageInfoBT<SettlementInfoListResponse>> listPage(@RequestBody @Valid SettlementInfoListRequest request) {
        Page<SettlementInfoListResponse> page = PageFactory.defaultPage();
        return ReturnUtil.success(super.packForBT(settlementInfoService.listPage(request, page)));
    }

    @ApiOperation("业务结算列表")
    @PostMapping("/list")
    public ReturnDTO<List<SettlementInfoListResponse>> list(@RequestBody @Valid SettlementInfoListRequest request) {
        return ReturnUtil.success(settlementInfoService.list(request));
    }

    @ApiOperation("业务结算新增")
    @PostMapping("/add")
    @ResponseBody
    public ReturnDTO add(@RequestBody @Valid FinanceBusinessSettlementInfo settlementInfo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return settlementInfoService.addSettlement(settlementInfo);
    }

    @ApiOperation("业务结算编辑")
    @PostMapping("/update")
    @ResponseBody
    public ReturnDTO update(@RequestBody @Valid FinanceBusinessSettlementInfo settlementInfo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return settlementInfoService.updateSettlement(settlementInfo);
    }

    @ApiOperation("业务结算详情")
    @PostMapping("/detail")
    @ResponseBody
    public ReturnDTO detail(@RequestBody @Valid IdDTO idDTO) {
        Assert.isFalse(Objects.isNull(idDTO.getId()), () -> {
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        });
        return settlementInfoService.settlementDetail(idDTO.getId());
    }

    @ApiOperation("业务结算导出")
    @PostMapping("/listExport")
    @ResponseBody
    public void export(@RequestBody @Valid SettlementInfoListRequest request) throws IOException {
        List<SettlementInfoListResponse> list;
        if (!CollectionUtils.isEmpty(request.getIds())) {
            list = settlementInfoService.listBySettlementIds(request.getIds());
        } else {
            list = settlementInfoService.list(request);
        }
        super.exportExcel(SettlementInfoListResponse.class, list, ExportEnum.SETTLEMENT_INFO_EXPORT.getSheetName());
    }

    @ApiOperation("业务结算删除")
    @PostMapping("/delete")
    @ResponseBody
    public ReturnDTO delete(@RequestBody @Valid IdsDTO idsDTO) {
        Assert.isFalse(CollectionUtils.isEmpty(idsDTO.getIds()), () -> {
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        });
        if(settlementInfoService.removeByIds(idsDTO.getIds())) {
            return ReturnUtil.success();
        }
        throw new BusinessException(BusinessExceptionEnum.HANDLE_FAILED);
    }
}
