package cn.stylefeng.guns.zjxl.cnotroller;

import cn.stylefeng.guns.zjxl.model.ZjxlArticleContent;
import cn.stylefeng.guns.zjxl.model.ZjxlCompanyCase;
import cn.stylefeng.guns.zjxl.service.ZjxlArticleContentService;
import cn.stylefeng.guns.zjxl.service.ZjxlCompanyCaseService;
import cn.stylefeng.guns.zjxlUtil.BaseController;
import cn.stylefeng.guns.zjxlUtil.PageUtil;
import com.alibaba.fastjson.JSONObject;
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: ZjxlCompanyCaseController
 * @Description:
 * @Auther: shi kejing
 * @Date: 2020/11/2 9:21
 */
@RestController
@Api(description = "公司官网主页-公司案例")
@RequestMapping(value = "/zjxl/CompanyCase")
public class ZjxlCompanyCaseController extends BaseController {

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

    @Autowired
    private ZjxlCompanyCaseService companyCaseService;
    @Autowired
    private ZjxlArticleContentService articleContentService;

    @RequestMapping(value = "/findCompanyCaseById", method = RequestMethod.GET)
    @ApiOperation(value = "查看CompanyCase")
    public String findCompanyCaseById(@ApiParam(name = "id", value = "companyId", required = false) @RequestParam(value = "id", required = false)String id,
                                      @ApiParam(name = "name", value = "companyName", required = false) @RequestParam(value = "name", required = false)String name,
                                      @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<ZjxlCompanyCase> companyCaseList = companyCaseService.findById(id,name,pageNo-1,pageSize);
            return write(200,"查询成功","data", PageUtil.getPage(companyCaseList,pageNo,pageSize,companyCaseService.allCount()));
        }catch (Exception e){
            e.printStackTrace();
            return write(-1,"查询失败");
        }
    }

    @RequestMapping(value = "/addCompanyCase", method = RequestMethod.POST)
    @ApiOperation(value = "添加公司案例")
    public String addCompanyCase(
            @ApiParam(name = "companyName", value = "案例名称", required = false) @RequestParam(value = "companyName", required = false)String companyName,
            @ApiParam(name = "companyDescribe", value = "案例描述", required = false) @RequestParam(value = "companyDescribe", required = false)String companyDescribe,
            @ApiParam(name = "companyDefaultImg", value = "默认图片", required = false) @RequestParam(value = "companyDefaultImg", required = false)String companyDefaultImg,
            @ApiParam(name = "companyExchangeImg", value = "交互图片", required = false) @RequestParam(value = "companyExchangeImg", required = false)String companyExchangeImg,
            @ApiParam(name = "companyAssociatedCase", value = "关联案例(对应的文章id)", required = false) @RequestParam(value = "companyAssociatedCase", required = false)String companyAssociatedCase,
            @ApiParam(name = "companyJumpUrl", value = "跳转链接", required = false) @RequestParam(value = "companyJumpUrl", required = false)String companyJumpUrl,
            @ApiParam(name = "companySort", value = "排序", required = false) @RequestParam(value = "companySort", required = false)Integer companySort,
            @ApiParam(name = "companyIsLine", value = "是否上线", required = false, defaultValue = "0") @RequestParam(value = "companyIsLine", required = false)Integer companyIsLine,
            @ApiParam(name = "companyTestBannerId", value = "测试用字段", required = false, defaultValue = "1") @RequestParam(value = "companyTestBannerId", required = false)String companyTestBannerId){
        try {
            ZjxlCompanyCase companyCase = new ZjxlCompanyCase();
            companyCase.setCompanyName(companyName);
            companyCase.setCompanyDescribe(companyDescribe);
            companyCase.setCompanyDefaultImg(companyDefaultImg);
            companyCase.setCompanyExchangeImg(companyExchangeImg);
            companyCase.setCompanyJumpUrl(companyJumpUrl);
            companyCase.setCompanySort(companySort);
            companyCase.setCompanyIsLine(companyIsLine);
            companyCase.setCompanyTestBannerId(companyTestBannerId);

            List<ZjxlArticleContent> caseA = articleContentService.findArticleContentById(companyAssociatedCase);
            if (caseA.size()>0){
                JSONObject object = new JSONObject();
                object.put("articleContentId",caseA.get(0).getArticleContentId());
                object.put("articleContentType",caseA.get(0).getArticleContentType());
                object.put("articleContentClassify",caseA.get(0).getArticleContentClassify());
                object.put("articleContentSubclassify",caseA.get(0).getArticleContentSubclassify());
                companyCase.setCompanyAssociatedCase(object.toString());
            }else {
                return write(-1,"添加案例不存在");
            }

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

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

    @RequestMapping(value = "/deleteCompanyCase", method = RequestMethod.POST)
    @ApiOperation(value = "根据id删除案例")
    public String deleteCompanyCase(String id){
        try{
            if (id == null){
                logger.info("删除合作伙伴 id===== "+id);
                return write(-1,"获取参数失败");
            }
            return write(200,"删除成功","data",companyCaseService.deleteCompanyCase(id));
        }catch (Exception e){
            e.printStackTrace();
            return write(-1,"删除失败");
        }
    }

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