package com.casic.missiles.controller.business;

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.ReturnDTO;
import com.casic.missiles.dto.ReturnUtil;
import com.casic.missiles.dto.business.interchange.*;
import com.casic.missiles.enums.BusinessExceptionEnum;
import com.casic.missiles.exception.BusinessException;
import com.casic.missiles.model.business.BusinessInterchange;
import com.casic.missiles.model.business.BusinessOrder;
import com.casic.missiles.model.exception.enums.CoreExceptionEnum;
import com.casic.missiles.service.business.IBusinessInterchangeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.BindingResult;
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.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Objects;

/**
 * <p>
 * 业务管理-设备交接单 前端控制器
 * </p>
 *
 * @author cz
 * @since 2023-02-02
 */
@RestController
@Api(tags = "设备交接单模块")
@RequestMapping("/business/interchange")
@AllArgsConstructor
public class BusinessInterchangeController extends ExportController {

    private final IBusinessInterchangeService bizExchangeService;

    @ApiOperation("设备交接单查询")
    @PostMapping("/listPage")
    public ReturnDTO<PageInfoBT<InterchangeListResponse>> exchangeListPage(@RequestBody @Valid InterchangeListRequest request, BindingResult bindingResult) throws Exception {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        Page<InterchangeListResponse> page = PageFactory.defaultPage();
        return ReturnUtil.success(super.packForBT(bizExchangeService.exchangeListPage(page, request)));
    }

    @ApiOperation("设备交接单详情")
    @PostMapping("/detail")
    public ReturnDTO<BusinessInterchangeDetailResponse> exchangeDetail(@RequestBody @Valid IdDTO idDTO) {
        Assert.isFalse(Objects.isNull(idDTO.getId()), () -> {
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        });
        return ReturnUtil.success(bizExchangeService.exchangeDetail(idDTO.getId()));
    }

    @ApiOperation("设备交接单更新(暂定只可以更新交接单，样品明细不可动)")
    @PostMapping("/update")
    public ReturnDTO updateExchange(@RequestBody @Valid BusinessInterchange businessInterchange, BindingResult bindingResult) {
        Assert.isFalse(Objects.isNull(businessInterchange.getId()), () -> {
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        });
        Assert.isFalse(bindingResult.hasErrors(), () -> {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        });
        return ReturnUtil.success(bizExchangeService.updateExchange(businessInterchange));
    }

    @ApiOperation("设备交接单新增")
    @PostMapping("/add")
    public ReturnDTO addExchange(@RequestBody @Valid BusinessInterchange businessInterchange, BindingResult bindingResult) {
        Assert.isFalse(bindingResult.hasErrors(), () -> {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        });
        return ReturnUtil.success(bizExchangeService.addExchange(businessInterchange));
    }

    @ApiOperation("设备交接单删除")
    @PostMapping("/delete")
    public ReturnDTO deleteExchange(@RequestBody @Valid IdDTO idDTO) {
        Assert.isFalse(Objects.isNull(idDTO.getId()), () -> {
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        });
        return bizExchangeService.deleteExchange(idDTO.getId());
    }

    @ApiOperation("设备交接单导出")
    @PostMapping("/export")
    public void exportExchange(@RequestBody @Valid InterchangeListRequest request, HttpServletResponse response) {
        bizExchangeService.exportExchange(request, response);
    }

}
