package com.casic.missiles.controller.meter;

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.ReturnDTO;
import com.casic.missiles.dto.ReturnUtil;
import com.casic.missiles.dto.meter.OrganizeListRequest;
import com.casic.missiles.dto.meter.ParentDeptRequest;
import com.casic.missiles.enums.BusinessExceptionEnum;
import com.casic.missiles.enums.ExportEnum;
import com.casic.missiles.exception.BusinessException;
import com.casic.missiles.model.exception.enums.CoreExceptionEnum;
import com.casic.missiles.model.meter.MeterOrganize;
import com.casic.missiles.service.meter.IMeterOrganizeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
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.ResponseBody;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * @Description: 计量组织管理
 * @Author: wangpeng
 * @Date: 2022/11/21 20:31
 */
@Api(tags = "计量组织管理接口")
@Controller
@RequestMapping("/meter/organize")
public class MeterOrganizeController extends ExportController {
    @Autowired
    private IMeterOrganizeService organizeService;

    /**
     * 计量组织列表
     */
    @ApiOperation("计量组织列表（分页）")
    @PostMapping("/listPage")
    @ResponseBody
    public ReturnDTO<PageInfoBT<MeterOrganize>> listPage(@RequestBody OrganizeListRequest request) throws Exception {
        Page<MeterOrganize> page = PageFactory.defaultPage();
        return ReturnUtil.success(super.packForBT(organizeService.listPage(page, request)));
    }


    @ApiOperation("计量组织列表")
    @PostMapping("/list")
    @ResponseBody
    public ReturnDTO<List<MeterOrganize>> list(@RequestBody OrganizeListRequest request) throws Exception {
        return ReturnUtil.success(organizeService.list(request));
    }

    @ApiOperation("计量组织导出")
    @PostMapping("/listExport")
    @ResponseBody
    public void export(@RequestBody OrganizeListRequest request) throws Exception {
        super.exportExcel(MeterOrganize.class, organizeService.list(request), ExportEnum.METER_ORGANIZE_EXPORT.getSheetName());
    }

    /**
     * 计量组织编辑
     * 编辑的组织名字同步更新到sys_dept
     */
    @ApiOperation("计量组织更新")
    @PostMapping("/update")
    @ResponseBody
    public ReturnDTO organizeUpdate(@RequestBody @Valid MeterOrganize meterOrganize, BindingResult bindingResult) {
        Assert.isFalse(Objects.isNull(meterOrganize.getId()), () -> {
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        });
        Assert.isFalse(bindingResult.hasErrors(), () -> {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        });
        return organizeService.updateOrganize(meterOrganize);
    }

    /**
     * 根据deptId查询组织信息和父级组织信息
     */
    @ApiOperation("查询组织信息及父级组织信息")
    @PostMapping("/deptInfo")
    @ResponseBody
    public ReturnDTO<PageInfoBT<MeterOrganize>> deptInfo(@RequestBody ParentDeptRequest request) {
        return ReturnUtil.success(organizeService.deptInfo(request));
    }

    /**
     * 计量组织详情
     * 基础信息列表取即可；
     * 主要人员（计量人员中查询）、仪器（即测量设备，测量设备中接口查询）、标准装置（标准装置中接口查询）；
     */

}
