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.po.InspectionConfig;
import com.chushang.inspection.project.query.InsConfigQuery;
import com.chushang.inspection.project.service.InspectionConfigService;
import com.chushang.security.annotation.RequiresPermissions;
import com.chushang.security.utils.SecurityUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * 电子巡检单配置
 * @auther: zhao
 * @date: 2024/6/20 9:57
 */
@RestController
@RequestMapping("/ins/config")
public class InsConfigController {

    @Resource
    InspectionConfigService configService;

    /**
     * 分页查询
     *
     * @param query 条件
     * @return 分页信息
     */
    @GetMapping("/page")
    @RequiresPermissions("ins:config:page")
    public AjaxResult findPage(@Validated InsConfigQuery query) {
        return AjaxResult.success(configService.pageList(query));
    }

    /**
     * 获取巡检单类型详细信息
     *
     * @param configId 字典类型id
     * @return 详细信息
     */
    @GetMapping("/info/{configId}")
    @RequiresPermissions("ins:config:info")
    public AjaxResult get(@PathVariable Long configId) {
        return AjaxResult.success(configService.getById(configId));
    }

    /**
     * 别名查询巡检单信息
     *
     * @return 详细信息
     */
    @GetMapping("/alias/{alias}")
    @RequiresPermissions("ins:config:info")
    public AjaxResult get(@PathVariable String alias) {
        return AjaxResult.success(configService.getByAlias(alias));
    }

    /**
     * 新增
     */
    @SysLog(value = "巡检单配置", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    @RequiresPermissions("wrk:config:save")
    public AjaxResult save(@RequestBody@Validated(Create.class) InspectionConfig config) {
        config.setConfigId(null);
        config.setDeptId(SecurityUtils.getDeptId());
        String alias = config.getAlias();
        LambdaQueryWrapper<InspectionConfig> configSql = WrapperUtils.builder();
        configSql.eq(InspectionConfig::getAlias, alias);
        long count = configService.count(configSql);
        AssertUtil.invalidate(count > 0 , "巡检单别名重复");
        configService.saveEntity(config);
        return AjaxResult.success(config.getConfigId());
    }

    /**
     * 修改
     */
    @SysLog(value = "巡检单配置", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @RequiresPermissions("wrk:config:update")
    public AjaxResult update(@RequestBody @Validated(Update.class) InspectionConfig config) {
        Long configId = config.getConfigId();
        AssertUtil.invalidate(null == configId, "未找寻到对应的巡检单配置");
        String alias = config.getAlias();
        LambdaQueryWrapper<InspectionConfig> configSql = WrapperUtils.builder();
        configSql.eq(InspectionConfig::getAlias, alias)
                .ne(InspectionConfig::getConfigId, configId);
        long count = configService.count(configSql);
        AssertUtil.invalidate(count > 0 , "巡检单别名重复");

        configService.updateByEntityId(config);
        return AjaxResult.success(config.getConfigId());
    }
}
