package com.bweb.controller.cms;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.security.SessionObject;
import com.hfkj.common.utils.OrderUtil;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.entity.AuditMsg;
import com.hfkj.entity.CmsContent;
import com.hfkj.entity.GoodsBrand;
import com.hfkj.entity.GoodsMsg;
import com.hfkj.model.ResponseData;
import com.hfkj.model.SecUserSessionObject;
import com.hfkj.service.cms.CmsContentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value="/cms")
@Api(value="cms")
public class CmsContentController {

    Logger log = LoggerFactory.getLogger(CmsContentController.class);

    @Resource
    private CmsContentService cmsContentService;

    @RequestMapping(value="/editCms",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "编辑CMS")
    public ResponseData editCms(@RequestBody CmsContent body, HttpServletRequest request) {
        try {

            if (body == null
                    || body.getPlatformCode() == null
                    || StringUtils.isBlank(body.getName())
                    || StringUtils.isBlank(body.getCode())) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            CmsContent cmsContent;

            if (body.getId() != null) {
                // 查询CMS
                cmsContent = cmsContentService.queryDetail(body.getId());
                if (cmsContent == null) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.CONTENT_NOT_FOUND, "");
                }
            } else {
                cmsContent = new CmsContent();
                cmsContent.setCreateTime(new Date());
            }

            cmsContent.setUpdateTime(new Date());
            cmsContent.setStatus(1);
            cmsContent.setName(body.getName());
            cmsContent.setAppid(body.getAppid());
            cmsContent.setJumpType(body.getJumpType());
            cmsContent.setJumpUrl(body.getJumpUrl());
            cmsContent.setPlatformCode(body.getPlatformCode());
            cmsContent.setImg(body.getImg());
            cmsContent.setCode(body.getCode());

            if (body.getId() != null) {
                cmsContentService.update(cmsContent);
            } else {
                cmsContentService.create(cmsContent);
            }

            return ResponseMsgUtil.success("操作成功");

        } catch (Exception e) {
            log.error("error!",e);
            return ResponseMsgUtil.exception(e);
        }

    }

    @RequestMapping(value="/getCms",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询列表")
    public ResponseData getCms(@RequestParam(value = "code" , required = false) String code,
                               @RequestParam(value = "name" , required = false) String name,
                               @RequestParam(value = "status" , required = false) Integer status,
                               @RequestParam(value = "platformCode" , required = false) String platformCode,
                               @RequestParam(value = "pageNum" , required = true) Integer pageNum,
                               @RequestParam(value = "pageSize" , required = true) Integer pageSize) {
        try {

            Map<String , Object> map = new HashMap<>();
            map.put("code", code);
            map.put("platformCode", platformCode);
            map.put("name", name);
            map.put("status", status);
            PageHelper.startPage(pageNum, pageSize);
            return ResponseMsgUtil.success(new PageInfo<>(cmsContentService.getList(map)));

        } catch (Exception e) {
            log.error("error!",e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/delete",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "删除")
    public ResponseData delete(@RequestParam(value = "id" , required = false) Long id) {
        try {

            cmsContentService.delete(id , false);

            return ResponseMsgUtil.success("删除成功");

        } catch (Exception e) {
            log.error("error!",e);
            return ResponseMsgUtil.exception(e);
        }
    }


    @RequestMapping(value="/cmsUpDown",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "商品上下架")
    public ResponseData cmsUpDown(@RequestParam(value = "id" , required = false) Long id, HttpServletRequest request) {
        try {

            CmsContent cmsContent = cmsContentService.queryDetail(id);

            if (cmsContent.getStatus() == 1) {
                cmsContent.setStatus(2);
                cmsContent.setUpdateTime(new Date());
            } else if (cmsContent.getStatus() == 2) {
                cmsContent.setStatus(1);
                cmsContent.setUpdateTime(new Date());

            }

            cmsContentService.update(cmsContent);

            return ResponseMsgUtil.success("成功");

        } catch (Exception e) {
            log.error("error!",e);
            return ResponseMsgUtil.exception(e);
        }
    }

}
