package com.bweb.controller;


import com.hai.common.exception.ErrorCode;
import com.hai.common.exception.ErrorHelp;
import com.hai.common.exception.SysCode;
import com.hai.common.utils.ResponseMsgUtil;
import com.hai.entity.CmsPatch;
import com.hai.model.ResponseData;
import com.hai.service.CmsPatchService;
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 java.util.Date;

@Controller
@RequestMapping(value = "/cmsPatch")
@Api(value = "内容管理->附件")
public class CmsPatchController {

    private static Logger log = LoggerFactory.getLogger(CmsPatchController.class);
    @Resource
    private CmsPatchService cmsPatchService;

    @RequestMapping(value = "/addPatch", method = RequestMethod.POST)
    @ApiOperation(value = "添加 附件")
    @ResponseBody
    public ResponseData addPatch(@RequestBody CmsPatch cmsPatch) {
        try {
            if (cmsPatch == null
                    || cmsPatch.getContentId() == null
                    || StringUtils.isBlank(cmsPatch.getPatchName())
                    || cmsPatch.getPatchType() == null
                    || StringUtils.isBlank(cmsPatch.getPatchPath())
            ) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            cmsPatch.setAddTime(new Date());
            if (cmsPatchService.addPatch(cmsPatch) > 0) {
                return ResponseMsgUtil.success("添加成功");
            } else {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.ADD_DATA_ERROR, "");
            }
        } catch (Exception e) {
            log.error("CmsPatchController --> addPatch() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

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

}
