package com.chushang.inspection.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chushang.common.core.exception.utils.AssertUtil;
import com.chushang.common.core.validator.Create;
import com.chushang.common.core.validator.Update;
import com.chushang.common.core.web.AjaxResult;
import com.chushang.common.log.annotation.SysLog;
import com.chushang.common.log.enums.BusinessType;
import com.chushang.common.mybatis.utils.WrapperUtils;
import com.chushang.inspection.project.dto.Delete;
import com.chushang.inspection.project.po.InspectionDetail;
import com.chushang.inspection.project.query.InsDetailQuery;
import com.chushang.inspection.project.service.InspectionDetailService;
import com.chushang.security.annotation.RequiresPermissions;
import com.chushang.security.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 巡检单配置详情
 * @auther: zhao
 * @date: 2024/6/20 17:27
 */
@Slf4j
@RestController
@RequestMapping("/ins/detail")
public class InsDetailController {

    @Resource
    InspectionDetailService detailService;

    /**
     * 配置详情查询
     *
     * @param query 条件
     * @return 分页信息
     */
    @GetMapping("/page")
    @RequiresPermissions("ins:detail:page")
    public AjaxResult findPage(@Validated InsDetailQuery query) {
        return AjaxResult.success(detailService.findPage(query));
    }

    /**
     * 获取字典类型详细信息
     *
     * @return 详细信息
     */
    @GetMapping("/info/{detailId}")
    @RequiresPermissions("ins:details:info")
    public AjaxResult get(@PathVariable Long detailId) {
        return AjaxResult.success(detailService.getById(detailId));
    }

    /**
     * 添加
     *
     */
    @SysLog(value = "巡检单配置详情", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    @RequiresPermissions("ins:details:save")
    public AjaxResult save(@RequestBody @Validated(Create.class) InspectionDetail detail) {
        detail.setDetailId(null);
        Long configId = detail.getConfigId();
        AssertUtil.invalidate(null == configId, "巡检单配置id 不能为空");

        String configKey = detail.getConfigKey();
        AssertUtil.invalidate(null == configKey, "巡检单配置key 不能为空");

        LambdaQueryWrapper<InspectionDetail> detailSql = WrapperUtils.builder();
        detailSql.eq(InspectionDetail::getConfigId, configId)
                        .eq(InspectionDetail::getConfigKey, configKey);
        long count = detailService.count(detailSql);
        AssertUtil.invalidate(count > 0, "巡检单配置key 重复");

        detailService.saveEntity(detail);

        return AjaxResult.success();
    }

    /**
     * 修改
     */
    @SysLog(value = "巡检单配置详情", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @RequiresPermissions("ins:details:update")
    public AjaxResult update(@RequestBody @Validated(Update.class) InspectionDetail detail) {
        Long detailId = detail.getDetailId();
        AssertUtil.invalidate(null == detailId, "detail_id 不能为空");
        Long configId = detail.getConfigId();
        AssertUtil.invalidate(null == configId, "configId 不能为空");
        String configKey = detail.getConfigKey();
        AssertUtil.invalidate(null == configKey, "巡检单配置key 不能为空");
        LambdaQueryWrapper<InspectionDetail> detailSql = WrapperUtils.builder();
        detailSql.eq(InspectionDetail::getConfigId, configId)
                .eq(InspectionDetail::getConfigKey, configKey)
                .ne(InspectionDetail::getDetailId, detailId);
        long count = detailService.count(detailSql);
        AssertUtil.invalidate(count > 0, "巡检单配置key 重复");

        detailService.updateByEntityId(detail);
        return AjaxResult.success();
    }

    /**
     * 删除
     */
    @SysLog(value = "巡检单配置详情", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    @RequiresPermissions("ins:details:delete")
    public AjaxResult delete(@RequestBody @Validated Delete delete) {
        detailService.removeByEntityIds(delete.getIds());
        return AjaxResult.success();
    }

}
