package com.yunzhi.marketing.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunzhi.marketing.base.BaseController;
import com.yunzhi.marketing.base.ResponseBean;
import com.yunzhi.marketing.common.CommConstant;
import com.yunzhi.marketing.entity.TaShareContent;
import com.yunzhi.marketing.service.TaShareContentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
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.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * <p>
    *   前端控制器
    * </p>
 *
 * @author jobob
 * @since 2019-09-21
 */
@RestController
@RequestMapping("/api")
public class TaShareContentController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(TaShareContentController.class);

    @Autowired
    public TaShareContentService iShareContentService;


    /**
     * 分页查询列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value="/shareContent",method= RequestMethod.GET)
    public ResponseBean shareContentList(@RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                         @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize){
        ResponseBean responseBean = new ResponseBean();
        try {
            //使用分页插件
		    IPage<TaShareContent> pg = new Page<>(pageNum, pageSize);
            QueryWrapper<TaShareContent> queryWrapper = new QueryWrapper<>();
            queryWrapper.orderByDesc("create_date");

            IPage<TaShareContent> result = iShareContentService.page(pg, queryWrapper);
            responseBean.addSuccess(result);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("shareContentList -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 保存对象
     * @param shareContent 实体对象
     * @return
     */
    @RequestMapping(value="/admin/shareContent",method= RequestMethod.POST)
    public ResponseBean shareContentAdd(@RequestBody TaShareContent shareContent, HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            shareContent.setStatus(CommConstant.STATUS_NORMAL);
            shareContent.setOrgId(getOrgId(request));
            shareContent.setCreateDate(LocalDateTime.now());
            if (iShareContentService.save(shareContent)){
                responseBean.addSuccess(shareContent);
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("shareContentAdd -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 根据id删除对象
     * @param id  实体ID
     */
    @ResponseBody
    @RequestMapping(value="/shareContent/{id}", method= RequestMethod.DELETE)
    public ResponseBean shareContentDelete(@PathVariable Integer id){
        ResponseBean responseBean = new ResponseBean();
        try {
            if(iShareContentService.removeById(id)){
                responseBean.addSuccess("success");
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("shareContentDelete -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 修改对象
     * @param id  实体ID
     * @param shareContent 实体对象
     * @return
     */
    @RequestMapping(value="/admin/shareContent/{id}",method= RequestMethod.PUT)
    public ResponseBean shareContentUpdate(@PathVariable Integer id,
                                        @RequestBody TaShareContent shareContent){
        ResponseBean responseBean = new ResponseBean();
        shareContent.setShareContentId(id);
        try {
            if (iShareContentService.updateById(shareContent)){
                responseBean.addSuccess(shareContent);
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("shareContentUpdate -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 查询对象
     */
    @RequestMapping(value="/admin/shareContent",method= RequestMethod.GET)
    public ResponseBean shareContentGet(@RequestParam String targetId,@RequestParam String targetType ){
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean.addSuccess(iShareContentService.getPostersForTarget(targetId,targetType));
        }catch (Exception e){
            e.printStackTrace();
            logger.error("shareContentDelete -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }
}
