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.siteExecutive.SiteExecuteLogRequest;
import com.casic.missiles.enums.BusinessExceptionEnum;
import com.casic.missiles.exception.BusinessException;
import com.casic.missiles.model.business.BusinessSiteExecutiveLog;
import com.casic.missiles.model.exception.enums.CoreExceptionEnum;
import com.casic.missiles.service.business.IBusinessSiteExecutiveLogService;
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-03-25
 */
@Api(tags = "现场检测记录模块")
@RestController
@RequestMapping("/business/siteExecutive/log")
public class BusinessSiteExecutiveLogController extends ExportController {
    @Resource
    private IBusinessSiteExecutiveLogService siteExecutiveLogService;

    @ApiOperation("现场检测记录表查询")
    @PostMapping("/listPage")
    public ReturnDTO<PageInfoBT<BusinessSiteExecutiveLog>> siteExecuteLogListPage(@RequestBody @Valid SiteExecuteLogRequest request, BindingResult bindingResult) throws Exception {
        Assert.isFalse(bindingResult.hasErrors(), () -> {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        });
        Page<BusinessSiteExecutiveLog> page = PageFactory.defaultPage();
        return ReturnUtil.success(super.packForBT(siteExecutiveLogService.siteExecutiveLogListPage(page, request)));
    }


    @ApiOperation("现场检测记录表详情")
    @PostMapping("/detail")
    public ReturnDTO<BusinessSiteExecutiveLog> originalDetail(@RequestBody @Valid IdDTO idDTO) throws Exception{
        Assert.isFalse(Objects.isNull(idDTO.getId()), () -> {
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        });
        return ReturnUtil.success(siteExecutiveLogService.siteExecutiveLogDetail(idDTO.getId()));
    }

    @ApiOperation("现场检测记录表更新")
    @PostMapping("/update")
    public ReturnDTO updateSiteExecutiveLog(@RequestBody @Valid BusinessSiteExecutiveLog businessSiteExecutiveLog, BindingResult bindingResult) {
        Assert.isFalse(Objects.isNull(businessSiteExecutiveLog.getId()), () -> {
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        });
        Assert.isFalse(bindingResult.hasErrors(), () -> {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        });
        return ReturnUtil.success(siteExecutiveLogService.updateSiteExecutiveLog(businessSiteExecutiveLog));
    }

    @ApiOperation("现场检测记录表新增")
    @PostMapping("/add")
    public ReturnDTO addSiteExecutiveLog(@RequestBody @Valid BusinessSiteExecutiveLog businessSiteExecutiveLog, BindingResult bindingResult) {
        Assert.isFalse(bindingResult.hasErrors(), () -> {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        });
        return ReturnUtil.success(siteExecutiveLogService.addSiteExecutiveLog(businessSiteExecutiveLog));
    }

    @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(siteExecutiveLogService.deleteSiteExecutiveLog(idDTO.getId()));
    }

    @ApiOperation("现场检测记录表导出")
    @PostMapping("/export")
    public void exportExecuteLog(@RequestBody @Valid SiteExecuteLogRequest request,HttpServletResponse response) {
        siteExecutiveLogService.exportExecuteLog(request, response);
    }

}
