package com.storlead.system.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.storlead.framework.common.result.Result;
import com.storlead.system.pojo.dto.SysDictDTO;
import com.storlead.system.pojo.entity.SysDictDetailEntity;
import com.storlead.system.pojo.entity.SysDictEntity;
import com.storlead.system.pojo.entity.SystemPersonalizeSettingEntity;
import com.storlead.system.service.SysDictDetailService;
import com.storlead.system.service.SysDictService;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * <p>
 * 字典组 前端控制器
 * </p>
 *
 * @author chenkq
 * @since 2022-09-09
 */
@RestController
@RequestMapping("/sys/dict")
@Api(tags="平台中心 - 系统: 数据字典")
public class SysDictApiController {

    @Resource
    private SysDictService sysDictService;

    @Resource
    private SysDictDetailService sysDictDetailService;

    @PostMapping(value = "/saveDict")
    @ApiOperation(value = "保存字典组", notes = "保存字典组")
    @ApiResponses({
            @ApiResponse(code = 200, message = "", response = SystemPersonalizeSettingEntity.class)
    })
    public Result saveDict(SysDictEntity sysDict) {
        if (StrUtil.isBlank(sysDict.getCode())) {
            return Result.error("参数错误,字典编码不能为空");
        }
        SysDictEntity entity = sysDictService.getOne(new LambdaQueryWrapper<SysDictEntity>().eq(SysDictEntity::getCode,sysDict.getCode()));
        if (Objects.nonNull(entity)) {
            if (Objects.isNull(sysDict.getDictId())) {
                return Result.error("字典"+sysDict.getCode()+"已被占用,请修改编码!");
            }else {
                if (!entity.getDictId().equals(sysDict.getDictId())) {
                    return Result.error("字典"+sysDict.getCode()+"已被占用编码!");
                }
            }
        }
        sysDictService.saveOrUpdate(sysDict);
        return Result.ok();
    }

    @PostMapping(value = "/saveDictDetail")
    @ApiOperation(value = "保存字典详情", notes = "保存字典详情")
    @ApiResponses({
            @ApiResponse(code = 200, message = "")
    })
    public Result saveDictDetail(SysDictDetailEntity Detail) {
        sysDictDetailService.saveOrUpdate(Detail);
        return Result.ok();
    }

    @PostMapping(value = "/pageDict")
    @ApiOperation(value = "查询字典配置数据", notes = "查询字典配置数据")
    @ApiResponses({
            @ApiResponse(code = 200, message = "")
    })
    public Result pageDict(SysDictDTO dto) {
        Page<SysDictEntity> page = new Page<>(dto.getPageIndex(),dto.getPageSize());
        LambdaQueryWrapper<SysDictEntity> queryWrapper = new LambdaQueryWrapper();
        if (StrUtil.isNotBlank(dto.getCode()) && StrUtil.isNotBlank(dto.getName())) {
            queryWrapper.eq(SysDictEntity::getCode, dto.getCode()).or().like(SysDictEntity::getName, dto.getName());
        } else {
            if (StrUtil.isNotBlank(dto.getCode())) {
                queryWrapper.eq(SysDictEntity::getCode, dto.getCode());
            } else if (StrUtil.isNotBlank(dto.getName())) {
                queryWrapper.like(SysDictEntity::getName, dto.getName());
            }
        }
        if (Objects.nonNull(dto.getDictType())) {
            queryWrapper.eq(SysDictEntity::getDictType, dto.getDictType());
        }
        queryWrapper.like(SysDictEntity::getIsDelete, Integer.valueOf(0));
        IPage<SysDictEntity> vos = sysDictService.page(page,queryWrapper);
        return Result.result(vos);
    }

    @PostMapping(value = "/listDetialByDictId")
    @ApiOperation(value = "获取字典详情", notes = "获取指定字典详情")
    @ApiResponses({
            @ApiResponse(code = 200, message = "")
    })
    public Result lisDetialByDictId(SysDictDTO dto) {
        if (Objects.isNull(dto.getDictId())) {
            return Result.error("查询参数错误!");
        }
        Page<SysDictDetailEntity> page = new Page<>(dto.getPageIndex(),dto.getPageSize());
        LambdaQueryWrapper<SysDictDetailEntity> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(SysDictDetailEntity::getDictId,dto.getDictId());
        queryWrapper.eq(SysDictDetailEntity::getIsDelete,Integer.valueOf(0));
//      queryWrapper.eq(SysDictDetailEntity::getEnabled,1);
        IPage<SysDictDetailEntity> vos = sysDictDetailService.page(page,queryWrapper);
        return Result.result(vos);
    }

    @PostMapping(value = "/listDetialByCodes")
    @ApiOperation(value = "获取字典组", notes = "获取字典组")
    @ApiResponses({
            @ApiResponse(code = 200, message = "", response = SysDictDetailEntity.class)
    })
    public Result lisDetialByCodes(SysDictDTO dto) {

        return sysDictService.lisDetialByCodes(dto);

    }

    @PostMapping(value = "/deleteDict")
    @ApiOperation(value = "删除字典组", notes = "删除字典组")
    @ApiResponses({
            @ApiResponse(code = 200, message = "", response = SysDictDetailEntity.class)
    })
    public Result deleteDict(@RequestParam(value = "ids") @ApiParam(value = "主键id") Long [] ids) {

        LambdaUpdateWrapper<SysDictEntity>  updateDictWrapper = new LambdaUpdateWrapper();
        updateDictWrapper.set(SysDictEntity::getIsDelete,Integer.valueOf(1));
        updateDictWrapper.in(SysDictEntity::getDictId,ids);
        sysDictService.update(updateDictWrapper);

        LambdaUpdateWrapper<SysDictDetailEntity> updateDictDetailWrapper = new LambdaUpdateWrapper();
        updateDictDetailWrapper.set(SysDictDetailEntity::getIsDelete,Integer.valueOf(1));
        updateDictDetailWrapper.in(SysDictDetailEntity::getDictId,ids);

        sysDictDetailService.update(updateDictDetailWrapper);
        return Result.ok();
    }

    @PostMapping(value = "/deleteDictDetail")
    @ApiOperation(value = "删除字典详情", notes = "删除字典详情")
    @ApiResponses({
            @ApiResponse(code = 200, message = "", response = SysDictDetailEntity.class)
    })
    public Result deleteDictDetail(@RequestParam(value = "ids") @ApiParam(value = "主键id") Long [] ids) {

        LambdaUpdateWrapper<SysDictDetailEntity> updateDictDetailWrapper = new LambdaUpdateWrapper();
        updateDictDetailWrapper.set(SysDictDetailEntity::getIsDelete,Integer.valueOf(1));
        updateDictDetailWrapper.in(SysDictDetailEntity::getDictDetailId,ids);

        sysDictDetailService.update(updateDictDetailWrapper);
        return Result.ok();
    }

    @ApiOperation("禁用或启用字典")
    @PostMapping("enabledDetail")
    public Result enabledTemplateDetail(Long id,Boolean enable) {
        LambdaUpdateWrapper<SysDictDetailEntity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(SysDictDetailEntity::getEnabled,enable);
        updateWrapper.eq(SysDictDetailEntity::getDictDetailId,id);
        sysDictDetailService.update(updateWrapper);
        return Result.ok();
    }
}
