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.application.service.AbstractDictService;
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.environment.EnvironmentListRequest;
import com.casic.missiles.dto.business.environment.EnvironmentListResponse;
import com.casic.missiles.enums.BusinessExceptionEnum;
import com.casic.missiles.enums.meter.MeterDictEnum;
import com.casic.missiles.exception.BusinessException;
import com.casic.missiles.model.business.BusinessEnvironmentRecord;
import com.casic.missiles.model.exception.enums.CoreExceptionEnum;
import com.casic.missiles.service.business.IBusinessEnvironmentRecordService;
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/environmentRecord")
public class BusinessEnvironmentRecordController extends ExportController {

    @Resource
    private IBusinessEnvironmentRecordService businessEnvironmentRecordService;
    @Resource
    private AbstractDictService dictService;

    @ApiOperation("环境检测单查询")
    @PostMapping("/listPage")
    public ReturnDTO<PageInfoBT<EnvironmentListResponse>> environmentListPage(@RequestBody @Valid EnvironmentListRequest request, BindingResult bindingResult) throws Exception {
        Assert.isFalse(bindingResult.hasErrors(), () -> {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        });
        Page<EnvironmentListResponse> page = PageFactory.defaultPage();
        return ReturnUtil.success(super.packForBT(businessEnvironmentRecordService.environmentListPage(page, request)));
    }


    @ApiOperation("环境检测单详情")
    @PostMapping("/detail")
    public ReturnDTO<BusinessEnvironmentRecord> environmentDetail(@RequestBody @Valid IdDTO idDTO) {
        Assert.isFalse(Objects.isNull(idDTO.getId()), () -> {
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        });
        return ReturnUtil.success(businessEnvironmentRecordService.environmentDetail(idDTO.getId()));
    }

    @ApiOperation("环境检测单更新")
    @PostMapping("/update")
    public ReturnDTO updateAdvice(@RequestBody @Valid BusinessEnvironmentRecord businessEnvironmentRecord, BindingResult bindingResult) {
        Assert.isFalse(Objects.isNull(businessEnvironmentRecord.getId()), () -> {
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        });
        Assert.isFalse(bindingResult.hasErrors(), () -> {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        });
        return ReturnUtil.success(businessEnvironmentRecordService.updateEnvironment(businessEnvironmentRecord));
    }

    @ApiOperation("环境检测单新增")
    @PostMapping("/add")
    public ReturnDTO addAdvice(@RequestBody @Valid BusinessEnvironmentRecord businessEnvironmentRecord, BindingResult bindingResult) {
        Assert.isFalse(bindingResult.hasErrors(), () -> {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        });
        return ReturnUtil.success(businessEnvironmentRecordService.addEnvironment(businessEnvironmentRecord));
    }

    @ApiOperation("环境检测单删除")
    @PostMapping("/delete")
    public ReturnDTO deleteAdvice(@RequestBody @Valid IdDTO idDTO) {
        Assert.isFalse(Objects.isNull(idDTO.getId()), () -> {
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        });

        return ReturnUtil.success(businessEnvironmentRecordService.deleteEnvironment(idDTO.getId()));
    }

    @ApiOperation("环境检测单导出")
    @PostMapping("/export")
    public void exportEnvironment(@RequestBody @Valid EnvironmentListRequest request, HttpServletResponse response) {
        businessEnvironmentRecordService.environmentExport(request, response);
    }
    @ApiOperation("获取房间号")//业务表
    @PostMapping("/getDoors")
    public ReturnDTO getDoors() {
        return ReturnUtil.success(dictService.findInDictByCode(MeterDictEnum.DOOR_NUMBER));
    }
}
