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.advice.CustomerAdviceListRequest;
import com.casic.missiles.enums.BusinessExceptionEnum;
import com.casic.missiles.exception.BusinessException;
import com.casic.missiles.model.customer.CustomerAdviceInfo;
import com.casic.missiles.model.exception.enums.CoreExceptionEnum;
import com.casic.missiles.service.customer.CustomerAdviceService;
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 javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Objects;

@Api(tags = "客户投诉模块")
@RestController
@RequestMapping("/customer/advice")
@AllArgsConstructor
public class CustomerAdviceController extends ExportController {

    private final CustomerAdviceService customerAdviceService;

    @ApiOperation("客户投诉查询")
    @PostMapping("/listPage")
    public ReturnDTO<PageInfoBT<CustomerAdviceInfo>> adviceListPage(@RequestBody @Valid CustomerAdviceListRequest request, BindingResult bindingResult) throws Exception {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        Page<CustomerAdviceInfo> page = PageFactory.defaultPage();
        return ReturnUtil.success(super.packForBT(customerAdviceService.adviceListPage(page, request)));
    }

    @ApiOperation("客户投诉详情")
    @PostMapping("/detail")
    public ReturnDTO<CustomerAdviceInfo> adviceDetail(@RequestBody @Valid IdDTO idDTO)throws Exception {
        Assert.isFalse(Objects.isNull(idDTO.getId()), () -> {
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        });
        return ReturnUtil.success(customerAdviceService.adviceDetail(idDTO.getId()));
    }

    @ApiOperation("客户投诉更新")
    @PostMapping("/update")
    public ReturnDTO updateAdvice(@RequestBody @Valid CustomerAdviceInfo customerAdviceInfo, BindingResult bindingResult){
        Assert.isFalse(Objects.isNull(customerAdviceInfo.getId()), () -> {
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        });
        Assert.isFalse(bindingResult.hasErrors(), () -> {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        });
        return customerAdviceService.updateAdvice(customerAdviceInfo);
    }

    @ApiOperation("客户投诉新增")
    @PostMapping("/add")
    public ReturnDTO addAdvice(@RequestBody @Valid CustomerAdviceInfo customerAdviceInfo, BindingResult bindingResult) {
        Assert.isFalse(bindingResult.hasErrors(), () -> {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        });
        return customerAdviceService.addAdvice(customerAdviceInfo);
    }

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

    //*******************************************************************************************************************//
    @ApiOperation("客户投诉导出")
    @PostMapping("/export")
    public void exportAdvice(@RequestBody @Valid CustomerAdviceListRequest request, HttpServletResponse response) {
        customerAdviceService.adviceExport(request, response);
    }

}
