package com.bweb.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hai.common.exception.ErrorCode;
import com.hai.common.exception.ErrorHelp;
import com.hai.common.exception.SysCode;
import com.hai.common.security.SessionObject;
import com.hai.common.security.UserCenter;
import com.hai.common.utils.ResponseMsgUtil;
import com.hai.entity.CmsCategoryModule;
import com.hai.model.ResponseData;
import com.hai.model.UserInfoModel;
import com.hai.service.CmsCategoryModuleService;
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
@Api(value = "内容管理 模板")
@RequestMapping(value = "/cmsCategoryModule")
public class CmsCategoryModuleController {

    private static Logger log = LoggerFactory.getLogger(CmsCategoryModuleController.class);

    @Resource
    private UserCenter userCenter;

    @Resource
    private CmsCategoryModuleService cmsCategoryModuleService;

    @RequestMapping(value = "/addCategoryModule", method = RequestMethod.POST)
    @ApiOperation(value = "增加 模板")
    @ResponseBody
    public ResponseData addCategoryModule(@RequestBody CmsCategoryModule cmsCategoryModule,
                                          HttpServletRequest request
    ) {
        try {
            if (cmsCategoryModule == null
                    || cmsCategoryModule.getCategoryId() == null
                    || StringUtils.isBlank(cmsCategoryModule.getModuleName())
                    || StringUtils.isBlank(cmsCategoryModule.getModulePath())
            ) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            // 获取操作者
            SessionObject sessionObject = userCenter.getSessionObject(request);
            if(sessionObject == null){
                throw ErrorHelp.genException(SysCode.System, ErrorCode.SEC_USER_EXPIRED, "");
            }
            UserInfoModel userInfoModel = (UserInfoModel) sessionObject.getObject();

            cmsCategoryModule.setStatus(1);
            cmsCategoryModule.setCreateTime(new Date());
            cmsCategoryModule.setOpId(userInfoModel.getSecUser().getId());
            if (cmsCategoryModuleService.addCategoryModule(cmsCategoryModule) > 0) {
                return ResponseMsgUtil.success("添加成功");
            } else {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.ADD_DATA_ERROR, "");
            }

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

    @RequestMapping(value = "/updateCategoryModule", method = RequestMethod.POST)
    @ApiOperation(value = "修改 模板")
    @ResponseBody
    public ResponseData updateCategoryModule(@RequestBody CmsCategoryModule cmsCategoryModule) {
        try {
            if (cmsCategoryModule == null
                    || cmsCategoryModule.getId() == null
                    || cmsCategoryModule.getCategoryId() == null
                    || StringUtils.isBlank(cmsCategoryModule.getModuleName())
                    || StringUtils.isBlank(cmsCategoryModule.getModulePath())
            ) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            CmsCategoryModule categoryModule = cmsCategoryModuleService.getCategoryModuleById(cmsCategoryModule.getId());
            if (categoryModule == null || categoryModule.getStatus() == 0) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.CMS_CATEGORY_MODULE_NOT_FOUND, "");
            }

            cmsCategoryModule.setStatus(categoryModule.getStatus());
            cmsCategoryModule.setCreateTime(categoryModule.getCreateTime());
            cmsCategoryModule.setOpId(categoryModule.getOpId());
            if (cmsCategoryModuleService.updateCategoryModule(cmsCategoryModule) > 0) {
                return ResponseMsgUtil.success("修改成功");
            } else {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.UPDATE_DATA_ERROR, "");
            }
        } catch (Exception e) {
            log.error("CmsCategoryModuleController --> updateCategoryModule() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/delCategoryModule", method = RequestMethod.GET)
    @ApiOperation(value = "删除 模板")
    @ResponseBody
    public ResponseData delCategoryModule(@RequestParam(value = "id", required = true) Long id) {
        try {
            if (cmsCategoryModuleService.delCategoryModule(id) > 0) {
                return ResponseMsgUtil.success("删除成功");
            } else {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.DELETE_DATA_ERROR, "");
            }
        } catch (Exception e) {
            log.error("CmsCategoryModuleController --> delCategoryModule() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/getCategoryModuleById", method = RequestMethod.GET)
    @ApiOperation(value = "根据id 查询模板")
    @ResponseBody
    public ResponseData getCategoryModuleById(@RequestParam(value = "id", required = true) Long id) {
        try {
            return ResponseMsgUtil.success(cmsCategoryModuleService.getCategoryModuleById(id));
        } catch (Exception e) {
            log.error("CmsCategoryModuleController --> getCategoryModuleById() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/getModuleByCategoryId", method = RequestMethod.GET)
    @ApiOperation(value = "根据分类id 查询模板列表")
    @ResponseBody
    public ResponseData getModuleByCategoryId(@RequestParam(value = "categoryId", required = true) Long categoryId) {
        try {
            Map<String, Object> paramsMap = new HashMap<>();
            paramsMap.put("categoryId", categoryId);

            return ResponseMsgUtil.success(cmsCategoryModuleService.getListCategoryModule(paramsMap));
        } catch (Exception e) {
            log.error("CmsCategoryModuleController --> getCategoryModuleById() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/getListCategoryModule", method = RequestMethod.GET)
    @ApiOperation(value = "查询列表 模板")
    @ResponseBody
    public ResponseData getListCategoryModule(@RequestParam(value = "categoryId", required = false) Long categoryId,
                                              @RequestParam(value = "categoryCode", required = false) String categoryCode,
                                              @RequestParam(value = "moduleName", required = false) String moduleName,
                                              @RequestParam(value = "status", required = false) Integer status,
                                              @RequestParam(name = "pageNum", required = true) Integer pageNum,
                                              @RequestParam(name = "pageSize", required = true) Integer pageSize) {
        try {
            Map<String, Object> paramsMap = new HashMap<>();
            if (categoryId != null) {
                paramsMap.put("categoryId", categoryId);
            }
            if (StringUtils.isNotBlank(categoryCode)) {
                paramsMap.put("categoryCode", categoryCode);
            }
            if (StringUtils.isNotBlank(moduleName)) {
                paramsMap.put("moduleName", moduleName);
            }
            if (status != null) {
                paramsMap.put("status", status);
            }

            PageHelper.startPage(pageNum, pageSize);
            return ResponseMsgUtil.success(new PageInfo<>(cmsCategoryModuleService.getListCategoryModule(paramsMap)));
        } catch (Exception e) {
            log.error("CmsCategoryModuleController --> getListCategoryModule() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/updateStatusOfModule", method = RequestMethod.POST)
    @ApiOperation(value = "更新 模板状态")
    @ResponseBody
    public ResponseData updateStatusOfContent(@RequestBody JSONObject jsonObject) {
        try {
            Long id = jsonObject.getLong("id");
            Integer status = jsonObject.getInteger("status");

            if (id == null
                    || status == null
                    || (status != 1 && status != 2)
            ) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            CmsCategoryModule categoryModule = cmsCategoryModuleService.getCategoryModuleById(id);
            if (categoryModule == null || categoryModule.getStatus() == 0) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.CMS_CATEGORY_MODULE_NOT_FOUND, "");
            }

            categoryModule.setStatus(status);
            if (cmsCategoryModuleService.updateCategoryModule(categoryModule) > 0) {
                return ResponseMsgUtil.success("修改成功");
            } else {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.UPDATE_DATA_ERROR, "");
            }
        } catch (Exception e) {
            log.error("CmsContentController --> updateStatusOfContent() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

}
