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.Info.CustomerInfoResponse;
import com.casic.missiles.dto.customer.Info.CustomerListRequest;
import com.casic.missiles.dto.customer.Info.SampleRecordsResponse;
import com.casic.missiles.enums.BusinessExceptionEnum;
import com.casic.missiles.exception.BusinessException;
import com.casic.missiles.model.customer.CustomerInfo;
import com.casic.missiles.model.customer.CustomerInfoImport;
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.service.customer.CustomerService;
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")
@AllArgsConstructor
public class CustomerController extends ExportController {

    private final CustomerService customerService;

    @ApiOperation("客户基本信息分页列表")
    @PostMapping("/listPage")
    public ReturnDTO<PageInfoBT<CustomerInfoResponse>> listPage(@RequestBody @Valid CustomerListRequest request, BindingResult bindingResult) throws Exception {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        Page<CustomerInfoResponse> page = PageFactory.defaultPage();
        return ReturnUtil.success(super.packForBT(customerService.listPage(page, request)));
    }

    @ApiOperation("客户基本信息不分页列表")
    @PostMapping("/list")
    public ReturnDTO<List<CustomerInfoResponse>> customerList(@RequestBody @Valid CustomerListRequest request, BindingResult bindingResult) throws Exception {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return ReturnUtil.success(customerService.customerList(request));
    }

    @ApiOperation("客户新增")
    @PostMapping("/add")
    public ReturnDTO addCustomer(@RequestBody @Valid CustomerInfo customerInfo, BindingResult bindingResult) throws Exception{
        if (bindingResult.hasErrors()) {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return customerService.addCustomer(customerInfo);
    }

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

    @ApiOperation("客户更新")
    @PostMapping("/update")
    public ReturnDTO updateCustomer(@RequestBody @Valid CustomerInfo customerInfo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return customerService.updateCustomer(customerInfo);
    }

    @ApiOperation("客户详情")
    @PostMapping("/detail")
    public ReturnDTO<CustomerInfo> customerDetail(@RequestBody @Valid IdDTO idDTO) {
        Assert.isFalse(Objects.isNull(idDTO.getId()), () -> {
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        });
        return ReturnUtil.success(customerService.customerDetail(idDTO.getId()));
    }

    @ApiOperation("根据客户id查询样品记录-分页")
    @PostMapping("/sampleRecords")
    public ReturnDTO<PageInfoBT<SampleRecordsResponse>> sampleRecordsByCustomerId(@RequestBody @Valid IdDTO idDTO) {
        Assert.isFalse(Objects.isNull(idDTO.getId()), () -> {
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        });
        return ReturnUtil.success(super.packForBT(customerService.sampleRecordsByCustomerId(idDTO.getId())));
    }

    //产品暂定不做
//    @ApiOperation("根据客户id查询合同记录-分页")
//    @PostMapping("/contractRecords")
//    public ReturnDTO<PageInfoBT<ContractRecordsResponse>> contractRecordsByCustomerId(@RequestBody @Valid IdDTO idDTO) {
//        Assert.isFalse(Objects.isNull(idDTO.getId()), () -> {
//            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
//        });
//        return ReturnUtil.success(super.packForBT(customerService.contractRecordsByCustomerId(idDTO.getId())));
//    }

    @ApiOperation("客户删除")
    @PostMapping("/delete")
    public ReturnDTO deleteCustomer(@RequestBody @Valid IdDTO idDTO) {
        Assert.isFalse(Objects.isNull(idDTO.getId()), () -> {
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        });
        return customerService.deleteCustomer(idDTO.getId());
    }

//    @ApiOperation("根据客户id查询检定记录(在样品中实现，可复用)")
//    @PostMapping("/fake/interface")
//    public ReturnDTO<PageInfoBT<MeasureRecordsResponse>> measureRecordsBySampleId() {
//        return null;
//    }

    @ApiOperation("客户导出")
    @PostMapping("/export")
    public void sampleExport(@RequestBody @Valid CustomerListRequest request, HttpServletResponse response) {
        customerService.customerExport(request, response);
    }


    //*******************************************************************************************************************//
//    @ApiOperation("客户批量导入")
//    @PostMapping("/batchImport")
//    public ReturnDTO exportAdvice(@RequestBody @Valid CustomerAdviceInfo customerAdviceInfo, BindingResult bindingResult) {
//        Assert.isFalse(bindingResult.hasErrors(), () -> {
//            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
//        });
//        return null;
//    }

}
