package com.dxhy.config.controller;

import com.dxhy.api.model.config.sys.SysDictdetaEntity;
import com.dxhy.api.model.config.sys.SysDicttypeEntity;
import com.dxhy.common.aspect.SysLog;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.utils.PageUtils;
import com.dxhy.common.utils.R;
import com.dxhy.common.utils.ValidatorUtils;
import com.dxhy.jxpt.baseService.config.service.SysDictdetaService;
import com.dxhy.jxpt.baseService.config.service.SysDicttypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

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

/**
 * 数据字典
 *
 * @author jiaohongyang
 */
@RestController
@RequestMapping("sys/dict")
@Slf4j
public class SysDictController {

    @Resource
    private SysDicttypeService sysDicttypeService;
    @Resource
    private SysDictdetaService sysDictdetaService;

    /**
     * 列表
     */
    @RequestMapping("/typeList")
    public ResponseEntity<?> list(@RequestBody Map<String, Object> params) {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        PageUtils page = sysDicttypeService.queryPage(params);

        return ResponseEntity.ok(R.ok().put("page", page));
    }

    /**
     * 信息
     */
    @RequestMapping("/infoType/{id}")
    public ResponseEntity<?> info(@PathVariable("id") Long id) {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        SysDicttypeEntity dicttype = sysDicttypeService.queryDicttypeById(id);

        return ResponseEntity.ok(R.ok().put("dicttype", dicttype));
    }

    /**
     * 保存
     */
    @RequestMapping("/saveType")
    @SysLog("进项平台保存字典类型")
    public ResponseEntity<?> save(@RequestBody SysDicttypeEntity dict) {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        try {
            // 校验类型
            ValidatorUtils.validateEntity(dict);
            // 根据 code 获取信息
            Integer queryCode = sysDicttypeService.queryCode(dict.getDicttypecode(), null);
            if (queryCode > 0) {
                return ResponseEntity.ok(R.error("保存失败,字典编码重复！"));
            }
            sysDicttypeService.saveDicttype(dict);

            return ResponseEntity.ok(R.ok("保存成功"));
        } catch (Exception e) {
           log.info("业务处理异常：{}", e);
            return ResponseEntity.ok(R.error("保存失败"));
        }

    }

    /**
     * 修改
     */
    @RequestMapping("/updateType")
    @SysLog("进项平台修改字典类型")
    public ResponseEntity<?> update(@RequestBody SysDicttypeEntity dict) {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        try {
            // 校验类型
            ValidatorUtils.validateEntity(dict);
            // 根据 code 获取信息
            Integer queryCode = sysDicttypeService.queryCode(dict.getDicttypecode(), dict.getDicttypeid());
            if (queryCode > 0) {
                return ResponseEntity.ok(R.error("保存失败,字典编码重复！"));
            }
            sysDicttypeService.updateDictType(dict);
            return ResponseEntity.ok(R.ok("保存成功"));
        } catch (Exception e) {
           log.info("业务处理异常：{}", e);
            return ResponseEntity.ok(R.error("保存失败"));
        }
    }

    /**
     * 删除
     */
    @RequestMapping("/deleteType")
    @SysLog("进项平台删除字典类型")
    public ResponseEntity<?> delete(Long id) {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        try {
            // 根据id 查询数据信息
            Long detaCount = sysDictdetaService.queryDictDetaId(id);
            if (detaCount > 0L) {
                return ResponseEntity.ok(R.error("删除失败，请删除数据列信息！"));
            }

            sysDicttypeService.deleteDictType(id);
            return ResponseEntity.ok(R.ok("删除成功"));
        } catch (Exception e) {

            return ResponseEntity.ok(R.error("删除失败"));
        }

    }

    /**
     * 列表
     */
    @RequestMapping("/detaList")
    public ResponseEntity<?> detaList(@RequestBody Map<String, Object> params) {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        PageUtils page = sysDictdetaService.queryPage(params);

        return ResponseEntity.ok(R.ok().put("page", page));
    }

    /**
     * 信息
     */
    @RequestMapping("/infoDeta/{id}")
    public ResponseEntity<?> infoDeta(@PathVariable("id") Long id) {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        SysDictdetaEntity dictdeta = sysDictdetaService.queryDiceDeta(id);

        return ResponseEntity.ok(R.ok().put("dictdeta", dictdeta));
    }
    /**
     * 保存
     */
    @RequestMapping("/saveDeta")
    @SysLog("进项平台保存字典值")
    public ResponseEntity<?> saveDeta(@RequestBody SysDictdetaEntity dict) {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        try {
            // 校验类型
            ValidatorUtils.validateEntity(dict);
            Integer dictDetaCode =
                sysDictdetaService.queryDictDetaCode(dict.getDicttypecode(), dict.getDictcode(), null);

            if (dictDetaCode > 0) {
                return ResponseEntity.ok(R.ok("保存失败,业务编码重复！"));
            }
            // 保存数据
            sysDictdetaService.saveDictDeta(dict);
            // 保存redis
            sysDictdetaService.saveRedis(dict);

            return ResponseEntity.ok(R.ok("保存成功"));

        } catch (Exception e) {
           log.info("业务处理异常：{}", e);
            return ResponseEntity.ok(R.error("保存失败"));
        }
    }

    /**
     * 修改
     */
    @RequestMapping("/updateDeta")
    @SysLog("进项平台修改字典值")
    public ResponseEntity<?> updateDeta(@RequestBody SysDictdetaEntity dict) {
        DynamicContextHolder.push(DbConstant.BASICS_READ);

        try {
            // 校验类型
            ValidatorUtils.validateEntity(dict);
            Integer dictDetaCode =
                sysDictdetaService.queryDictDetaCode(dict.getDicttypecode(), dict.getDictcode(), dict.getDictid());

            if (dictDetaCode > 0) {
                return ResponseEntity.ok(R.ok("保存失败,业务编码重复！"));
            }
            sysDictdetaService.updateDictDeta(dict);
            // 保存redis
            sysDictdetaService.saveRedis(dict);
            return ResponseEntity.ok(R.ok("保存成功"));

        } catch (Exception e) {
           log.info("业务处理异常：{}", e);
            return ResponseEntity.ok(R.error("保存失败"));
        }
    }

    /**
     * 删除
     */
    @RequestMapping("/deleteDeta")
    @SysLog("进项平台删除字典值")
    public ResponseEntity<?> deleteDeta(@RequestBody Long[] ids) {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        try {
            sysDictdetaService.deleteRedis(ids);
            return ResponseEntity.ok(R.ok("删除成功"));
        } catch (Exception e) {
           log.info("业务处理异常：{}", e);
            return ResponseEntity.ok(R.error("删除失败"));
        }
    }

    /**
     * 刷新用户缓存
     */
    @RequestMapping(value = "/refreshCache", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<?> refreshCache() {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        if (sysDictdetaService.refreshCache()) {
            return ResponseEntity.ok(R.ok("刷新缓存成功！"));
        } else {
            return ResponseEntity.ok(R.error("刷新缓存失败，请稍后重试！"));
        }
    }
}
