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.TaCustomImg;
import com.yunzhi.marketing.service.ITaCustomImgService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

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

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

    @Autowired
    public ITaCustomImgService iTaCustomImgService;


    /**
     * 分页查询列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value="/admin/taCustomImg",method= RequestMethod.GET)
    public ResponseBean taCustomImgList(@RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                        @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize, HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            //使用分页插件
		    IPage<TaCustomImg> pg = new Page<>(pageNum, pageSize);
            QueryWrapper<TaCustomImg> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("org_id", getOrgId(request));
            queryWrapper.eq("status", CommConstant.STATUS_NORMAL);
            queryWrapper.orderByDesc("create_time");

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

    /**
     * 保存对象
     * @param taCustomImg 实体对象
     * @return
     */
    @RequestMapping(value="/admin/taCustomImg",method= RequestMethod.POST)
    public ResponseBean taCustomImgAdd(@RequestBody TaCustomImg taCustomImg, HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        taCustomImg.setImgDesc(CommConstant.BANNER_INDEX.equals(taCustomImg.getImgType()) ? "小程序首页分享配图" : "小程序首页推荐客户入口配图");
        try {
            taCustomImg.setCreateTime(LocalDateTime.now());
            taCustomImg.setUpdateTime(LocalDateTime.now());
            boolean countFlag = iTaCustomImgService.countCustomImg(taCustomImg, getOrgId(request));
            if (!countFlag){
                responseBean.addError("您已新增首页分享或推荐客户图，请勿重复添加");
                return responseBean;
            }

            taCustomImg.setStatus(CommConstant.STATUS_NORMAL);
            taCustomImg.setOrgId(getOrgId(request));

            List<TaCustomImg> taCustomImgs = new ArrayList<>();
            taCustomImgs.add(taCustomImg);
            if (CommConstant.IMG_TYPE.equals(taCustomImg.getImgType())){
                TaCustomImg customImg = new TaCustomImg();
                customImg.setCreateTime(LocalDateTime.now());
                customImg.setUpdateTime(LocalDateTime.now());
                customImg.setOrgId(getOrgId(request));
                customImg.setStatus(CommConstant.STATUS_NORMAL);
                customImg.setImgUrl("https://estateagents.oss-cn-shanghai.aliyuncs.com/miniapp/upload/images/1574145199853-97feda7895c65be33aa234a7b81b37f.jpg");
                customImg.setImgType(CommConstant.BANNER_INDEX);
                customImg.setImgDesc("小程序首页分享配图");
                taCustomImgs.add(customImg);
            }
            if (CommConstant.BANNER_INDEX.equals(taCustomImg.getImgType())){
                TaCustomImg customImg = new TaCustomImg();
                customImg.setCreateTime(LocalDateTime.now());
                customImg.setUpdateTime(LocalDateTime.now());
                customImg.setOrgId(getOrgId(request));
                customImg.setStatus(CommConstant.STATUS_NORMAL);
                customImg.setImgUrl("http://njcj.oss-cn-shanghai.aliyuncs.com/icon.png");
                customImg.setImgType(CommConstant.INDEX_NEWS);
                customImg.setImgDesc("首页资讯logo");
                taCustomImgs.add(customImg);
            }
            if (iTaCustomImgService.saveBatch(taCustomImgs)){
                responseBean.addSuccess(taCustomImg);
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taCustomImgAdd -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

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

    /**
     * 修改对象
     * @param taCustomImg 实体对象
     * @return
     */
    @RequestMapping(value="/admin/taCustomImg",method= RequestMethod.PUT)
    public ResponseBean taCustomImgUpdate(@RequestBody TaCustomImg taCustomImg, HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            taCustomImg.setUpdateTime(LocalDateTime.now());
            boolean countFlag = iTaCustomImgService.countCustomImg(taCustomImg, getOrgId(request));
            if (!countFlag){
                responseBean.addError("您已新增首页分享或推荐客户图，请勿重复添加");
                return responseBean;
            }

            if (iTaCustomImgService.updateById(taCustomImg)){
                responseBean.addSuccess(taCustomImg);
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taCustomImgUpdate -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 根据id查询对象
     * @param id  实体ID
     */
    @RequestMapping(value="/admin/taCustomImg/{id}",method= RequestMethod.GET)
    public ResponseBean taCustomImgGet(@PathVariable Integer id){
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean.addSuccess(iTaCustomImgService.getById(id));
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taCustomImgDelete -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 微信接口获取首页分享或推荐客户图
     */
    @RequestMapping(value="/wx/taCustomImg",method= RequestMethod.GET)
    public ResponseBean taCustomImgGet(HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            QueryWrapper<TaCustomImg> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("org_id", getOrgId(request));
            queryWrapper.eq("status", CommConstant.STATUS_NORMAL);
            List<TaCustomImg> taCustomImg = iTaCustomImgService.list(queryWrapper);
            responseBean.addSuccess(taCustomImg);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(" taCustomImgGet-=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }
}
