package cn.stylefeng.guns.zjxl.cnotroller;

import cn.stylefeng.guns.zjxl.model.ZjxlSystemDict;
import cn.stylefeng.guns.zjxl.service.ZjxlSystemDictService;
import cn.stylefeng.guns.zjxlUtil.BaseController;
import cn.stylefeng.guns.zjxlUtil.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
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.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/***
 * @ClassName: ZjxlSystemDictController
 * @Description:
 * @Auther: shi kejing
 * @Date: 2020/11/4 9:31
 */
@RestController
@RequestMapping(value = "/zjxl/systemDict")
@Api(description = "系统管理字典")
public class ZjxlSystemDictController extends BaseController {

    private Logger logger = (Logger) LoggerFactory.getLogger(ZjxlSystemDictController.class);

    @Autowired
    private ZjxlSystemDictService systemDictService;

    @RequestMapping(value = "/findSystemDictById", method = RequestMethod.GET)
    @ApiOperation("查询系统管理字典")
    public String findSystemDictById(            @ApiParam(name = "id", value = "systemDictId", required = false) @RequestParam(value = "id", required = false)String id,
//                                                 @ApiParam(name = "name", value = "partnername", required = false) @RequestParam(value = "name", required = false)String name,
                                                 @ApiParam(name = "systemDictType", value = "类型 1-产品与服务  2-公司案例  3-新闻中心  4-关于我们", required = false) @RequestParam(value = "systemDictType", required = false)Integer systemDictType,
                                                 @ApiParam(name = "pageNo", value = "第几页", defaultValue = "1") @RequestParam(value = "pageNo", required = false) Integer pageNo,
                                                 @ApiParam(name = "pageSize", value = "分页大小", defaultValue = "10") @RequestParam(value = "pageSize", required = false) Integer pageSize){
        try {
            List<ZjxlSystemDict> systemDictList = systemDictService.findSystemDictById(id,systemDictType,pageNo-1,pageSize);
            return write(200,"查询成功","data", PageUtil.getPage(systemDictList,pageNo,pageSize,systemDictService.allCount()));
        }catch (Exception e){
            e.printStackTrace();
            return write(-1,"查询失败");
        }
    }

    @RequestMapping(value = "/addSystemDict", method = RequestMethod.POST)
    @ApiOperation("添加系统管理字典")
    public String addSystemDict(
                                @ApiParam(name = "systemDictType", value = "类型 1-产品与服务  2-公司案例  3-新闻中心  4-关于我们", required = false)@RequestParam(value = "systemDictType", required = false) Integer systemDictType,
                                @ApiParam(name = "systemDictClassify", value = "分类", required = false)@RequestParam(value = "systemDictClassify", required = false) String systemDictClassify,
                                @ApiParam(name = "systemDictSubclassify", value = "子分类", required = false)@RequestParam(value = "systemDictSubclassify", required = false) String systemDictSubclassify,
                                @ApiParam(name = "systemDictSort", value = "排序", required = false)@RequestParam(value = "systemDictSort", required = false) Integer systemDictSort,
                                @ApiParam(name = "systemDictIsLine", value = "是否上线", required = false, defaultValue = "0")@RequestParam(value = "systemDictIsLine", required = false) Integer systemDictIsLine){
        try {
            ZjxlSystemDict systemDict = new ZjxlSystemDict();
            systemDict.setSystemDictType(systemDictType);
            //查询分类是否存在
            int systemDictClassifyList = systemDictService.findSystemDictClassify(systemDictClassify);
            //查询子分类是否存在
            int systemDictSubclassifyList = systemDictService.findSystemDictSubclassify(systemDictSubclassify);
            if (systemDictClassifyList>0){
                return write(-1,"该分类已存在，请勿重复添加");
            }
            if (systemDictClassifyList > 0 && systemDictSubclassifyList>0){
                return write(-1,"该子分类已存在，请勿重复添加");
            }
            systemDict.setSystemDictClassify(systemDictClassify);
            systemDict.setSystemDictSubclassify(systemDictSubclassify);
            systemDict.setSystemDictSort(systemDictSort);
            systemDict.setSystemDictIsLine(systemDictIsLine);
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            systemDict.setSystemDictCreateTime(df.format(new Date()));// new Date()为获取当前系统时间

            return write(200,"添加成功","data",systemDictService.addSystemDict(systemDict));
        }catch (Exception e){
            e.printStackTrace();
            return write(-1,"添加失败");
        }
    }

    @RequestMapping(value = "/deleteSystemDict", method = RequestMethod.POST)
    @ApiOperation(value = "删除字典")
    public String deleteSystemDict(@ApiParam(name = "id", value = "systemDictId", required = false) @RequestParam(value = "id", required = false)String id){
        try {
            if (id == null) {
                logger.info("删除合作伙伴：id====="+id);
                return write(-1,"获取参数失败");
            }
            return write(200,"删除成功","data",systemDictService.deleteSystemDict(id));
        }catch (Exception e){
            e.printStackTrace();
            return write(-1,"删除失败");
        }
    }

    @RequestMapping(value = "/updateSystemDict", method = RequestMethod.POST)
    @ApiOperation(value = "修改是否上线")
    public String updateSystemDict(@ApiParam(name = "id", value = "systemDictId", required = false) @RequestParam(value = "id", required = false)String id,
                                   @ApiParam(name = "isLine", value = "是否上线", required = false) @RequestParam(value = "isLine", required = false)Integer isLine){
        try {
            if (id == null || isLine == null){
                logger.info("修改是否上线：id====="+id+"，isLine===="+isLine);
                return write(-1,"获取参数失败");
            }
            return write(200,"修改上线成功","data",systemDictService.updateSystemDict(id,isLine));
        }catch (Exception e){
            e.printStackTrace();
            return write(-1,"修改上线失败");
        }
    }

    @RequestMapping(value = "/findNewsType", method = RequestMethod.GET)
    @ApiOperation("查询新闻中心分类")
    public String findNewsType( @ApiParam(name = "systemDictType", value = "类型 1-产品与服务  2-公司案例  3-新闻中心  4-关于我们", required = false) @RequestParam(value = "systemDictType", required = false)Integer systemDictType){
        try {
            List<ZjxlSystemDict> systemDictList = systemDictService.findNewsType(systemDictType);
            return write(200,"查询成功","data", systemDictList);
        }catch (Exception e){
            e.printStackTrace();
            return write(-1,"查询失败");
        }
    }

    @RequestMapping(value = "/findAllType", method = RequestMethod.GET)
    @ApiOperation("获取系统字典的所有分类")
    public String findAllType(){
        try {
            List<ZjxlSystemDict> systemDictList = systemDictService.findAllType();
            return write(200,"查询成功","data", systemDictList);
        }catch (Exception e){
            e.printStackTrace();
            return write(-1,"查询失败");
        }
    }

}
