package com.sunyard.ssp.modules.sysconf.dict.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sunyard.ssp.common.PageVo;
import com.sunyard.ssp.common.Result;
import com.sunyard.ssp.common.annotation.AuditControllerLog;
import com.sunyard.ssp.common.constant.SqlConstant;
import com.sunyard.ssp.modules.sysconf.dict.entity.ScDict;
import com.sunyard.ssp.modules.sysconf.dict.service.IScDictDataService;
import com.sunyard.ssp.modules.sysconf.dict.service.IScDictService;
import com.sunyard.ssp.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author fyc
 * @since 2020-03-13
 */
@Controller
@Slf4j
@RequestMapping("/sysconf/scDict")
@Transactional
@Api(description = "系统配置-字典管理", tags = "系统配置-字典管理")
public class ScDictController {

    @Autowired
    IScDictService iScDictService;

    @Autowired
    IScDictDataService iScDictDataService;

    @RequestMapping(value = "/search", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation("根据字典关键字查询")
    public Result<List<ScDict>> search(@RequestParam String key) {
        List<ScDict> scDicts = iScDictService.selectByKey(key);
        return new ResultUtil<List<ScDict>>().setData(scDicts);

    }

    @RequestMapping(value = "/getById", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation("根据ID获取字典")
    public Result<ScDict> get(@RequestParam String id) {
        ScDict entity = iScDictService.getById(id);
        return new ResultUtil<ScDict>().setData(entity);
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation("获取所以字典（不分页）")
    public Result<List<ScDict>> getAll() {

        List<ScDict> list = iScDictService.list(new QueryWrapper<ScDict>().orderByAsc(SqlConstant.DB_SORT_COLUMN));
        return new ResultUtil<List<ScDict>>().setData(list);
    }

    @RequestMapping(value = "/page", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation("获取所有字典（分页）")
    public Result<IPage<ScDict>> getByPage(@ModelAttribute PageVo page) {
        Page<ScDict> data = new Page<>(page.getPageNumber(), page.getPageSize());
        IPage<ScDict> iPage = iScDictService.page(data);
        return new ResultUtil<IPage<ScDict>>().setData(iPage);
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("添加字典")
    @AuditControllerLog(description = "添加字典", operateType = "新增")
    public Result<ScDict> save(@ModelAttribute ScDict entity) {
        Map checkData = iScDictService.checkInputForm(entity);
        if (!(Boolean) checkData.get("status")) {
            return new ResultUtil<ScDict>().setErrorMsg((String) checkData.get("message"));
        }
        try {
            boolean e = iScDictService.save(entity);
            if (!e) {
                return new ResultUtil<ScDict>().setErrorMsg("添加失败");
            }
            return new ResultUtil<ScDict>().setSuccessMsg("添加成功");
        } catch (DuplicateKeyException e) {
            return new ResultUtil<ScDict>().setErrorMsg("字典名称或字典类型已经存在");
        }
    }

    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    @ResponseBody
    @ApiOperation("更新字典")
    @AuditControllerLog(description = "更新字典", operateType = "更新")
    public Result<UpdateChainWrapper<ScDict>> update(@ModelAttribute ScDict entity) {
        Map checkData = iScDictService.checkInputForm(entity);
        if (!(Boolean) checkData.get("status")) {
            return new ResultUtil<UpdateChainWrapper<ScDict>>().setErrorMsg((String) checkData.get("message"));
        }
        try {
            boolean e = iScDictService.updateById(entity);
            if (!e) {
                return new ResultUtil<UpdateChainWrapper<ScDict>>().setErrorMsg("更新失败");
            }
            return new ResultUtil<UpdateChainWrapper<ScDict>>().setSuccessMsg("更新成功");
        } catch (DuplicateKeyException e) {
            return new ResultUtil<UpdateChainWrapper<ScDict>>().setErrorMsg("字典名称或字典类型已经存在");
        }
    }

    @RequestMapping(value = "/deleteByIds", method = RequestMethod.DELETE)
    @ResponseBody
    @ApiOperation("删除字典（批量）")
    @AuditControllerLog(description = "删除字典", operateType = "删除")
    public Result<Object> delAllByIds(@RequestParam String[] ids) {

        for (String id : ids) {
            iScDictService.removeById(id);
            iScDictDataService.deleteByDictId(Long.valueOf(id));
        }
        return new ResultUtil<Object>().setSuccessMsg("批量通过id删除数据成功");
    }
}
