package com.casic.missiles.controller.customer;

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.customer.sample.*;
import com.casic.missiles.dto.customer.MeasureRecordsResponse;
import com.casic.missiles.enums.BusinessExceptionEnum;
import com.casic.missiles.exception.BusinessException;
import com.casic.missiles.model.customer.CustomerSampleInfo;
import com.casic.missiles.model.customer.CustomerSampleInfoImport;
import com.casic.missiles.model.exception.enums.CoreExceptionEnum;
import com.casic.missiles.model.meter.MeterTraceSupplier;
import com.casic.missiles.model.meter.MeterTraceSupplierImport;
import com.casic.missiles.service.customer.CustomerSampleService;
import com.casic.missiles.utils.ConvertUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

@Api(tags = "客户样本信息模块")
@RestController
@RequestMapping("/customer/sample")
@AllArgsConstructor
public class CustomerSampleController extends ExportController {

    private final CustomerSampleService customerSampleService;

    @ApiOperation("样品列表查询-分页")
    @PostMapping("/listPage")
    public ReturnDTO<PageInfoBT<CustomerSampleListResponse>> listPage(@RequestBody @Valid CustomerSampleListRequest request, BindingResult bindingResult) throws Exception {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        Page<CustomerSampleListResponse> page = PageFactory.defaultPage();
        return ReturnUtil.success(super.packForBT(customerSampleService.listPage(page, request)));
    }

    @ApiOperation("样品新增")
    @PostMapping("/add")
    public ReturnDTO addSample(@RequestBody @Valid CustomerSampleInfo customerSampleInfo, BindingResult bindingResult) throws Exception {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return customerSampleService.addSample(customerSampleInfo);
    }

    @ApiOperation("批量导入样品")
    @PostMapping(value = "/import", headers = "content-type=multipart/form-data")
    @ResponseBody
    public ReturnDTO importBatch(@RequestParam MultipartFile[] multipartFile) throws Exception {
        List<CustomerSampleInfoImport> sourceList = new ArrayList();
        for (MultipartFile file : multipartFile) {
            List<CustomerSampleInfoImport> list =  super.importExcel(CustomerSampleInfoImport.class, file, null, null);
            sourceList.addAll(list);
        }
        List<CustomerSampleInfo> targetList = ConvertUtils.sourceToTarget(sourceList, CustomerSampleInfo.class);
        return customerSampleService.addSampleList(targetList);
    }

    @ApiOperation("样品编辑")
    @PostMapping("/update")
    public ReturnDTO updateSample(@RequestBody @Valid CustomerSampleInfo customerSampleInfo, BindingResult bindingResult) {
        Assert.isFalse(Objects.isNull(customerSampleInfo.getId()), () -> {
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        });
        if (bindingResult.hasErrors()) {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return customerSampleService.updateSample(customerSampleInfo);
    }

    @ApiOperation("样品详情")
    @PostMapping("/detail")
    public ReturnDTO<CustomerSampleInfo> sampleDetail(@RequestBody @Valid IdDTO idDTO) {
        Assert.isFalse(Objects.isNull(idDTO.getId()), () -> {
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        });
        return ReturnUtil.success(customerSampleService.sampleDetail(idDTO.getId()));
    }

    @ApiOperation("列表数据删除")
    @PostMapping("/delete")
    public ReturnDTO deleteSample(@RequestBody @Valid IdDTO idDTO) {
        Assert.isFalse(Objects.isNull(idDTO.getId()), () -> {
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        });
        return customerSampleService.deleteSample(idDTO.getId());
    }

    @ApiOperation("样品导出")
    @PostMapping("/export")
    public void sampleExport(@RequestBody @Valid CustomerSampleListRequest request, HttpServletResponse response) {
        customerSampleService.sampleExport(request, response);
    }

    @ApiOperation("根据样品id/客户id查询检定记录")
    @PostMapping("/measureRecordsById")
    public ReturnDTO<PageInfoBT<MeasureRecordsResponse>> measureRecordsById(@RequestBody CustomerSampleIdRequest customerSampleIdRequest) {
        return ReturnUtil.success(super.packForBT(customerSampleService.measureRecordsById(customerSampleIdRequest)));
    }

    @ApiOperation("样品列表（委托书页面）")
    @PostMapping("/listPageByOrder")
    public ReturnDTO<PageInfoBT<MultiItemSampleListResponse>> listPageByOrder(@RequestBody @Valid OrderSampleListRequest request, BindingResult bindingResult) throws Exception {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        Page<MultiItemSampleListResponse> page = PageFactory.defaultPage();
        return ReturnUtil.success(super.packForBT(customerSampleService.listPageByOrder(page, request)));
    }

    @ApiOperation("样品列表（交接单页面）")
    @PostMapping("/listPageByInterchange")
    public ReturnDTO<PageInfoBT<MultiItemSampleListResponse>> listPageByInterchange(@RequestBody @Valid InterchangeSampleListRequest request, BindingResult bindingResult) throws Exception {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        Page<MultiItemSampleListResponse> page = PageFactory.defaultPage();
        return ReturnUtil.success(super.packForBT(customerSampleService.listPageByInterchange(page, request)));
    }

    @ApiOperation("样品列表（分包页面）")
    @PostMapping("/listPageBySubpackage")
    public ReturnDTO<PageInfoBT<MultiItemSampleListResponse>> listPageBySubpackage(@RequestBody @Valid SubpackageSampleListRequest request, BindingResult bindingResult) throws Exception {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        Page<MultiItemSampleListResponse> page = PageFactory.defaultPage();
        return ReturnUtil.success(super.packForBT(customerSampleService.listPageBySubpackage(page, request)));
    }

    @ApiOperation("样品列表（分包证书页面）")
    @PostMapping("/listPageBySubpackageCertificate")
    public ReturnDTO<PageInfoBT<MultiItemSampleListResponse>> listPageBySubpackageCertificate(@RequestBody @Valid InterchangeSampleListRequest request, BindingResult bindingResult) throws Exception {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        Page<MultiItemSampleListResponse> page = PageFactory.defaultPage();
        return ReturnUtil.success(super.packForBT(customerSampleService.listPageBySubpackageCertificate(page, request)));
    }


}
