package cn.stylefeng.guns.zjxl.cnotroller;

import cn.stylefeng.guns.zjxl.model.ZjxlArticleContent;
import cn.stylefeng.guns.zjxl.model.ZjxlSystemDict;
import cn.stylefeng.guns.zjxl.service.ZjxlArticleContentService;
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 io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
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.Iterator;
import java.util.List;

/***
 * @ClassName: ZjxlArticleContentController
 * @Description:文章内容管理
 * @Auther: shi kejing
 * @Date: 2020/11/3 17:40
 */
@RestController
@Api(description = "文章内容管理")
@RequestMapping(value = "/zjxl/zjxlArticleContent")
public class ZjxlArticleContentController extends BaseController {

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

    @Autowired
    private ZjxlArticleContentService articleContentService;

    @Autowired
    private ZjxlSystemDictService systemDictService;

    @RequestMapping(value = "/findArticleContentById", method = RequestMethod.GET)
    @ApiOperation(value = "查询文章管理列表")
    public String findArticleContentById(@ApiParam(name = "id", value = "文章id", required = false) @RequestParam(value = "id", required = false)String id,
                                         @ApiParam(name = "articleContentTitle", value = "文章标题", required = false) @RequestParam(value = "articleContentTitle", required = false)String articleContentTitle,
                                         @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<ZjxlArticleContent> articleContentList = articleContentService.findArticleContentById(id,articleContentTitle,systemDictType,pageNo-1,pageSize);
            return write(200,"查询成功","data", PageUtil.getPage(articleContentList,pageNo,pageSize,articleContentService.allCount()));
        }catch (Exception e){
            e.printStackTrace();
            return write(-1,"查询失败");
        }
    }

    @RequestMapping(value = "/addArticleContent", method = RequestMethod.POST)
    @ApiOperation(value = "添加文章")
    public String addArticleContent(
            @ApiParam(name = "articleContentTitle", value = "文章标题", required = false) @RequestParam(value = "articleContentTitle", required = false)String articleContentTitle,
            @ApiParam(name = "articleContentCover", value = "封面", required = false) @RequestParam(value = "articleContentCover", required = false)String articleContentCover,
            @ApiParam(name = "articleContentAbstract", value = "摘要", required = false) @RequestParam(value = "articleContentAbstract", required = false) String articleContentAbstract,
            @ApiParam(name = "articleContentContent", value = "内容", required = false) @RequestParam(value = "articleContentContent", required = false) String articleContentContent,
            @ApiParam(name = "articleContentType", value = "内容类型 1-产品与服务  2-公司案例  3-新闻中心  4-关于我们", required = false) @RequestParam(value = "articleContentType", required = false)Integer articleContentType,
            @ApiParam(name = "articleContentClassify", value = "分类", required = false) @RequestParam(value = "articleContentClassify", required = false)String articleContentClassify,
            @ApiParam(name = "articleContentSubclassify", value = "子分类", required = false) @RequestParam(value = "articleContentSubclassify", required = false)String articleContentSubclassify,
            @ApiParam(name = "articleContentSort", value = "排序", required = false) @RequestParam(value = "articleContentSort", required = false)Integer articleContentSort,
            @ApiParam(name = "articleContentIsLine", value = "是否上线", required = false, defaultValue = "0") @RequestParam(value = "articleContentIsLine", required = false)Integer articleContentIsLine){
        try {
            ZjxlArticleContent articleContent = new ZjxlArticleContent();

            List<ZjxlArticleContent> list = articleContentService.findArticleAll(null);
            if (list.size() > 0){
                Iterator<ZjxlArticleContent> iterator = list.iterator();
                while (iterator.hasNext()){
                    if (articleContentTitle.equals(iterator.next().getArticleContentTitle())){
                        return write(-1,"改文章已存在");
                    }
                }
            }

            articleContent.setArticleContentTitle(articleContentTitle);
            articleContent.setArticleContentCover(articleContentCover);
            articleContent.setArticleContentAbstract(articleContentAbstract);
            articleContent.setArticleContentType(articleContentType);
            articleContent.setArticleContentClassify(articleContentClassify);
            articleContent.setArticleContentSubclassify(articleContentSubclassify);
            articleContent.setArticleContentSort(articleContentSort);
            articleContent.setArticleContentIsLine(articleContentIsLine);
            articleContent.setArticleContentContent(articleContentContent);

            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            articleContent.setArticleContentCreateTime(df.format(new Date()));// new Date()为获取当前系统时间

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



    @RequestMapping(value = "/deleteArticleContent", method = RequestMethod.POST)
    @ApiOperation(value = "删除文章管理")
    public String deleteArticleContent(@ApiParam(name = "id", value = "文章id") @RequestParam(value = "id") String id){
        try {
            if (id == null){
                logger.info("修改是否上线：id====="+id);
                return write(-1,"获取参数失败");
            }
            logger.info("修改是否上线：id====="+id);
            return write(200,"删除成功","data",articleContentService.deleteArticleContent(id));
        }catch (Exception e){
            e.printStackTrace();
            return write(-1,"删除失败");
        }
    }

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

    @RequestMapping(value = "/getLeftTabs", method = RequestMethod.GET)
    @ApiOperation(value = "根据每个内容类型获取对应的分类")
    public String getLeftTabs(@ApiParam(name = "articleContentType", value = "内容类型 1-产品与服务  2-公司案例  3-新闻中心  4-关于我们") @RequestParam(value = "articleContentType") String articleContentType){
        try {
            if (StringUtils.isEmpty(articleContentType)){
                logger.info("内容类型：articleContentType====="+articleContentType);
                return write(-1,"获取参数失败");
            }
            logger.info("内容类型：articleContentType====="+articleContentType);
            return write(200,"操作成功","data",articleContentService.getLeftTabs(articleContentType));
        }catch (Exception e){
            e.printStackTrace();
            return write(-1,"操作失败");
        }
    }

    @RequestMapping(value = "/getArticleContentSubclassify", method = RequestMethod.GET)
    @ApiOperation(value = "根据每个分类获取对应的子分类")
    public String getArticleContentSubclassify(@ApiParam(name = "articleContentType", value = "内容类型 1-产品与服务  2-公司案例  3-新闻中心  4-关于我们") @RequestParam(value = "articleContentType") String articleContentType,
                                               @ApiParam(name = "articleContentClassify", value = "分类") @RequestParam(value = "articleContentClassify") String articleContentClassify){
        try {
            if (StringUtils.isEmpty(articleContentType) || StringUtils.isEmpty(articleContentClassify)){
                logger.info("内容类型：articleContentType====="+articleContentType);
                logger.info("分类：articleContentClassify====="+articleContentClassify);
                return write(-1,"获取参数失败");
            }
            logger.info("内容类型：articleContentType====="+articleContentType);
            logger.info("分类：articleContentClassify====="+articleContentClassify);
            List<ZjxlArticleContent> list = articleContentService.getArticleContentSubclassify(articleContentType,articleContentClassify);
            if (list.size() > 0){
                return write(200,"操作成功","data",list);
            }else {
                ZjxlArticleContent content = new ZjxlArticleContent();
                List<ZjxlSystemDict> systemDictList = systemDictService.findNewsTypeA(new Integer(articleContentType),articleContentClassify);
                if (systemDictList.size() > 0){
                    for (int i=0;i<systemDictList.size();i++){
                        content.setArticleContentSubclassify(systemDictList.get(i).getSystemDictSubclassify());
                        list.add(content);
                    }
                }
             return write(200,"操作成功","data",list);
            }
        }catch (Exception e){
            e.printStackTrace();
            return write(-1,"操作失败");
        }
    }

    @RequestMapping(value = "/getArticleContent", method = RequestMethod.GET)
    @ApiOperation(value = "根据子分类获取对应的文章(排序最高的文章)")
    public String getArticleContent(@ApiParam(name = "articleContentType", value = "内容类型 1-产品与服务  2-公司案例  3-新闻中心  4-关于我们") @RequestParam(value = "articleContentType") String articleContentType,
                                    @ApiParam(name = "articleContentClassify", value = "分类") @RequestParam(value = "articleContentClassify") String articleContentClassify,
                                    @ApiParam(name = "articleContentSubclassify", value = "子分类") @RequestParam(value = "articleContentSubclassify") String articleContentSubclassify){
        try {
            if (StringUtils.isEmpty(articleContentType) || StringUtils.isEmpty(articleContentClassify) || StringUtils.isEmpty(articleContentSubclassify)){
                logger.info("内容类型：articleContentType====="+articleContentType);
                logger.info("分类：articleContentClassify====="+articleContentClassify);
                logger.info("子分类：articleContentSubclassify====="+articleContentSubclassify);
                return write(-1,"获取参数失败");
            }
            logger.info("内容类型：articleContentType====="+articleContentType);
            logger.info("分类：articleContentClassify====="+articleContentClassify);
            logger.info("子分类：articleContentSubclassify====="+articleContentSubclassify);
            return write(200,"操作成功","data",articleContentService.getArticleContent(articleContentType,articleContentClassify,articleContentSubclassify));
        }catch (Exception e){
            e.printStackTrace();
            return write(-1,"操作失败");
        }
    }

    @RequestMapping(value = "/findNewsList", method = RequestMethod.GET)
    @ApiOperation(value = "查询新闻中心列表")
    public String findNewsList(@ApiParam(name = "id", value = "文章id", required = false) @RequestParam(value = "id", required = false)String id,
                                        // @ApiParam(name = "articleContentTitle", value = "文章标题", required = false) @RequestParam(value = "articleContentTitle", required = false)String articleContentTitle,
                                        // @ApiParam(name = "systemDictType", value = "内容类型 1-产品与服务  2-公司案例  3-新闻中心  4-关于我们", required = false, defaultValue = "3") @RequestParam(value = "systemDictType", required = false)Integer systemDictType,
                               @ApiParam(name = "articleContentClassify", value = "分类: 公司动态  媒体报道  参观交流", defaultValue = "公司动态") @RequestParam(value = "articleContentClassify") String articleContentClassify,
                               @ApiParam(name = "articleContentSubclassify", value = "子分类") @RequestParam(value = "articleContentSubclassify", required = false) String articleContentSubclassify,
                               @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<ZjxlArticleContent> articleContentList = articleContentService.findNewsList(id,articleContentClassify,articleContentSubclassify,3,pageNo-1,pageSize);
            return write(200,"查询成功","data", PageUtil.getPage(articleContentList,pageNo,pageSize,articleContentService.allNewsCount(articleContentClassify,articleContentSubclassify)));
        }catch (Exception e){
            e.printStackTrace();
            return write(-1,"查询失败");
        }
    }

    @RequestMapping(value = "/findArticleAll", method = RequestMethod.GET)
    @ApiOperation(value = "获取所有文章")
    public String findArticleAll(@ApiParam(name = "articleContentType", value = "子分类") @RequestParam(value = "articleContentType", required = false) String articleContentType){
        try {
            return write(200,"查询成功","data", articleContentService.findArticleAll(articleContentType));
        }catch (Exception e){
            e.printStackTrace();
            return write(-1,"查询失败");
        }
    }
}
