package com.dxhy.admin.sys.controller;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import com.dxhy.admin.sys.entity.SysDictdetaEntity;
import com.dxhy.admin.sys.entity.SysDicttypeEntity;
import com.dxhy.admin.sys.service.SysDictdetaService;
import com.dxhy.admin.sys.service.SysDicttypeService;
import com.dxhy.common.aspect.SysLog;
import com.dxhy.common.utils.PageUtils;
import com.dxhy.common.utils.R;
import com.dxhy.common.utils.ValidatorUtils;

import lombok.extern.slf4j.Slf4j;

/**
 * 数据字典
 *
 * @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) {
        PageUtils page = sysDicttypeService.queryPage(params);

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

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

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

    /**
     * 保存
     */
    @RequestMapping("/saveType")
    @SysLog("进项平台保存字典类型")
    public ResponseEntity<?> save(@RequestBody SysDicttypeEntity dict) {
        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) {
            e.printStackTrace();
            return ResponseEntity.ok(R.error("保存失败"));
        }

    }

    /**
     * 修改
     */
    @RequestMapping("/updateType")
    @SysLog("进项平台修改字典类型")
    public ResponseEntity<?> update(@RequestBody SysDicttypeEntity dict) {
        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) {
            e.printStackTrace();
            return ResponseEntity.ok(R.error("保存失败"));
        }
    }

    /**
     * 删除
     */
    @RequestMapping("/deleteType")
    @SysLog("进项平台删除字典类型")
    public ResponseEntity<?> delete(Long id) {
        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) {
        PageUtils page = sysDictdetaService.queryPage(params);

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

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

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

    /**
     * 保存
     */
    @RequestMapping("/saveDeta")
    @SysLog("进项平台保存字典值")
    public ResponseEntity<?> saveDeta(@RequestBody SysDictdetaEntity dict) {
        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) {
            e.printStackTrace();
            return ResponseEntity.ok(R.error("保存失败"));
        }
    }

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

        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) {
            e.printStackTrace();
            return ResponseEntity.ok(R.error("保存失败"));
        }
    }

    /**
     * 删除
     */
    @RequestMapping("/deleteDeta")
    @SysLog("进项平台删除字典值")
    public ResponseEntity<?> deleteDeta(@RequestBody Long[] ids) {
        try {
            sysDictdetaService.deleteRedis(ids);
            return ResponseEntity.ok(R.ok("删除成功"));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(R.error("删除失败"));
        }
    }

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