package com.blueland.sys.dict.web;


/**
 * @author 代码自动生成工具
 * @version 1.0
 * @since
 */

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blueland.annotation.Log;
import com.blueland.constant.RedisConstant;
import com.blueland.model.TPfDict;
import com.blueland.result.R;
import com.blueland.service.IDictService;
import com.blueland.service.SysRedisService;
import io.swagger.annotations.Api;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.blueland.bo.DictEntity;

import static com.blueland.constant.CommonConstant.*;


@Api(value = "dict")
@RestController
@RequestMapping(value = "/api/dict")
@Slf4j
public class DictRest {
    @Autowired
    private IDictService dictService;

    @Autowired
    SysRedisService sysRedisService;

    /**
     * 查询list-包含数据库相关信息
     */
    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public Page<DictEntity> listPage(@RequestParam Map<String, Object> queryParams) {
        queryParams.put("orderByColumns", "T1.SORT");
        Page<DictEntity> page = dictService.findDictPageListByParams(queryParams, Integer.parseInt(queryParams.get("pageNum").toString()), Integer.parseInt(queryParams.get("pageSize").toString()));
        return page;
    }

    /**
     * 搜索对象
     **/
    @RequestMapping(value = "", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    @CrossOrigin
    public R<Page<DictEntity>> list(DictEntity dictEntity) {
        Page<DictEntity> page = dictService.findPage(dictEntity);
        return R.ok(page);
    }


    /**
     * 查看对象
     **/
    @RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    @CrossOrigin
    public R<TPfDict> view(@PathVariable String id) {
        TPfDict dictEntity = dictService.getById(id);
        return R.ok(dictEntity);
    }


    /**
     * 查看对象（支持多条件查询）
     **/
    @RequestMapping(value = "get", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    @CrossOrigin
    public R<TPfDict> get(DictEntity model) {
        TPfDict dictEntity = dictService.get(model);
        return R.ok(dictEntity);
    }


    /**
     * 新增对象
     **/
    @Log(busiType = "新增字典数据", opType = SAVE, type = "web")
    @RequestMapping(value = "save", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    @CrossOrigin
    public R<DictEntity> save(@RequestBody DictEntity dict, HttpServletRequest request) {
        dictService.saveDict(dict);
        sysRedisService.saveOrUpdateDict(dict);
        return R.ok(dict);
    }


    /**
     * 更新对象
     *
     * @return
     **/
    @Log(busiType = "修改字典数据", opType = UPDATE, type = "web")
    @RequestMapping(value = "update", method = RequestMethod.PUT, produces = "application/json;charset=UTF-8")
    @ResponseBody
    @CrossOrigin
    public R<DictEntity> update(@RequestBody DictEntity model, HttpServletRequest request) {
        TPfDict dict = BeanUtil.copyProperties(model, TPfDict.class);
        dictService.updateById(dict);
        //20200428 add 更新缓存
        sysRedisService.saveOrUpdateDict(model);
        return R.ok(model);
    }

    /**
     * 根据ID删除对象（物理删除）
     *
     * @return
     **/
    @Log(busiType = "根据ID删除字典数据", opType = DELETE, type = "web")
    @RequestMapping(value = "delete/{id}")
    @ResponseBody
    @CrossOrigin
    public R<Boolean> delete(@PathVariable String id, HttpServletRequest request) {
        dictService.deleteDictById(id);
        //删除操作触发缓存刷新
        sysRedisService.flushDict();
        return R.ok();
    }

    /**
     * 根据编码删除对象（逻辑删除：修改状态）
     *
     * @return
     **/
    @Log(busiType = "根据编码删除对象（逻辑删除：修改状态）字典数据", opType = DELETE, type = "web")
    @RequestMapping(value = "deleteDicts/{dictCode}")
    @ResponseBody
    @CrossOrigin
    public R<Boolean> deleteDicts(@PathVariable String dictCode, HttpServletRequest request) {
        dictService.deleteDicts(dictCode);
        // 删除操作触发缓存刷新
        sysRedisService.flushDict();
        return R.ok();
    }


    /**
     * 批量删除对象
     *
     * @return
     **/
    @Log(busiType = "批量删除字典数据", opType = DELETE, type = "web")
    @RequestMapping(value = "deleteBatch", method = RequestMethod.DELETE, produces = "application/json;charset=UTF-8")
    @ResponseBody
    @CrossOrigin
    public R<Boolean> deleteBatch(String[] ids) {
        if (ObjectUtil.isEmpty(ids)) {
            throw new RuntimeException("id不能为空");
        }
        dictService.removeByIds(Arrays.asList(ids));
        // 删除操作触发缓存刷新
        sysRedisService.flushDict();;
        return R.ok();
    }


}



