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.originRecord.OriginRecordRequest;
import com.casic.missiles.enums.BusinessExceptionEnum;
import com.casic.missiles.exception.BusinessException;
import com.casic.missiles.model.business.BusinessOriginalRecord;
import com.casic.missiles.model.customer.CustomerSampleInfo;
import com.casic.missiles.model.exception.enums.CoreExceptionEnum;
import com.casic.missiles.service.business.IBusinessOriginalRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Objects;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zt
 * @since 2023-02-02
 */
@RestController
@Api(tags = "原始记录单模块")
@RequestMapping("/business/originalRecord")
public class BusinessOriginalRecordController extends ExportController {
    @Resource
    private IBusinessOriginalRecordService businessOriginalRecordService;

    @ApiOperation("原始记录查询")
    @PostMapping("/listPage")
    public ReturnDTO<PageInfoBT<BusinessOriginalRecord>> originalListPage(@RequestBody @Valid OriginRecordRequest request, BindingResult bindingResult) throws Exception {
        Assert.isFalse(bindingResult.hasErrors(), () -> {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        });
        Page<BusinessOriginalRecord> page = PageFactory.defaultPage();
        return ReturnUtil.success(super.packForBT(businessOriginalRecordService.originListPage(page, request)));
    }


    @ApiOperation("原始记录详情")
    @PostMapping("/detail")
    public ReturnDTO<BusinessOriginalRecord> originalDetail(@RequestBody @Valid IdDTO idDTO) throws Exception{
        Assert.isFalse(Objects.isNull(idDTO.getId()), () -> {
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        });
        return ReturnUtil.success(businessOriginalRecordService.originalDetail(idDTO.getId()));
    }

    @ApiOperation("原始记录更新")
    @PostMapping("/update")
    public ReturnDTO updateOriginal(@RequestBody @Valid BusinessOriginalRecord businessOriginalRecord, BindingResult bindingResult) {
        Assert.isFalse(Objects.isNull(businessOriginalRecord.getId()), () -> {
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        });
        Assert.isFalse(bindingResult.hasErrors(), () -> {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        });
        return ReturnUtil.success(businessOriginalRecordService.updateOriginal(businessOriginalRecord));
    }

    @ApiOperation("原始记录新增")
    @PostMapping("/add")
    public ReturnDTO addOriginal(@RequestBody @Valid BusinessOriginalRecord businessOriginalRecord, BindingResult bindingResult) {
        Assert.isFalse(bindingResult.hasErrors(), () -> {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        });
        return ReturnUtil.success(businessOriginalRecordService.addOriginal(businessOriginalRecord));
    }

    @ApiOperation("原始记录删除")
    @PostMapping("/delete")
    public ReturnDTO deleteOriginal(@RequestBody @Valid IdDTO idDTO) {
        Assert.isFalse(Objects.isNull(idDTO.getId()), () -> {
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        });
        return ReturnUtil.success(businessOriginalRecordService.deleteOriginal(idDTO.getId()));
    }

    @ApiOperation("原始记录导出")
    @PostMapping("/export")
    public void exportOriginal(@RequestBody @Valid OriginRecordRequest request, HttpServletResponse response) {
        businessOriginalRecordService.originExport(request, response);
    }


    @ApiOperation("根据委托单id查询关联样品信息(分页)")
    @PostMapping("/samplesByOderId")
    public ReturnDTO<PageInfoBT<CustomerSampleInfo>> samplesByOderId(@RequestBody @Valid OriginRecordRequest request)  {
        Page<CustomerSampleInfo> page = PageFactory.defaultPage();
        return ReturnUtil.success(super.packForBT(businessOriginalRecordService.samplesByOderId(page, request)));
    }

//    @ApiOperation("获取设备")
//    @PostMapping("/getEquipments")
//    public void getEquipments() {
////        businessEnvironmentRecordService.environmentExport(request, response);
//    }

}
