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


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.modules.sysconf.dict.entity.ScDictData;
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.RequestBody;
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.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;

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

    @Autowired
    IScDictDataService iScDictDataService;

    @Autowired
    IScDictService iScDictService;

    @RequestMapping(value = "/getById",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation("根据Id获取字典数据")
    public Result<ScDictData> get(@RequestParam String id){

        ScDictData entity = iScDictDataService.getById(id);
        log.info("根据Id获取字典数据");
        return new ResultUtil<ScDictData>().setData(entity);
    }

    @RequestMapping(value = "/selectByType",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation("根据类型获取字典数据")
    public Result<List<ScDictData>> selectByType(@RequestParam String type){
        List<ScDictData> list = iScDictDataService.selectByDictType(type);
        log.info("根据类型获取字典数据");
        return new ResultUtil<List<ScDictData>>().setData(list);
    }

    @RequestMapping(value = "/selectVersion",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation("根据类型获取系统版本号")
    public Result selectVersion(){
        List<ScDictData> list = iScDictDataService.selectByDictType("version");
        if (Objects.isNull(list)||list.size()<=0) {
            return new ResultUtil<>().setErrorMsg("未获取到版本号");
        }
        ScDictData scDictData = list.get(0);
        log.info("根据类型获取字典数据");
        return new ResultUtil<>().setData(scDictData.getValue());
    }

    @ApiOperation("条件查询接口")
    @RequestMapping(value = "/queryList",method = RequestMethod.POST)
    @ResponseBody
    public Result<IPage<ScDictData>> queryList(@RequestBody ScDictData scDictData){
        try {
            IPage<ScDictData> scDictDataIPage = iScDictDataService.queryList(scDictData);
            log.info("条件查询接口");
            return new ResultUtil<IPage<ScDictData>>().setData(scDictDataIPage);
        }catch (Exception e){
            log.error(e.getMessage());
            return null;
        }
    }

    @RequestMapping(value = "/getAll",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation("获取全部字典数据（不分页）")
    public Result<List<ScDictData>> getAll(){
        List<ScDictData> list = iScDictDataService.list();
        return new ResultUtil<List<ScDictData>>().setData(list);

    }

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

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

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

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

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

}
