package com.huiju.estateagents.sample.controller;

import com.alibaba.fastjson.JSONObject;
import com.huiju.estateagents.base.BaseController;
import com.huiju.estateagents.base.ResponseBean;
import com.huiju.estateagents.sample.entity.TaH5Sample;
import com.huiju.estateagents.sample.entity.TaNotice;
import com.huiju.estateagents.sample.service.ITaH5SampleService;
import com.huiju.estateagents.sample.service.ITaNoticeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 开屏通知  前端控制器
 * </p>
 *
 * @author fxf
 * @since 2020-03-18
 */
@RestController
@RequestMapping("/api")
public class TaNoticeController extends BaseController {

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

    @Autowired
    public ITaNoticeService iTaNoticeService;

    @Autowired
    public ITaH5SampleService iTaH5SampleService;

    /**
     * 条件查询通知列表
     *
     * @param pageNum
     * @param pageSize
     * @param title
     * @param targetType
     * @param targetName
     * @param status
     * @param type       类型 目前默认 sample
     * @return
     */
    @RequestMapping(value = "/channel/listNoticeByCondition", method = RequestMethod.GET)
    public ResponseBean listNoticeByCondition(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                              @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                              String title, String targetType, String targetName, Integer status,
                                              String type) {
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean = iTaNoticeService.listNoticeByCondition(pageNum, pageSize, title, targetType, targetName, status, type);
            logger.info("TaNoticeController.listNoticeByCondition 返回结果：" + JSONObject.toJSONString(responseBean));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("taNoticeList -=- {}", e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 保存对象
     *
     * @param taNotice 实体对象
     * @return
     */
    @RequestMapping(value = "/channel/taNotice", method = RequestMethod.POST)
    public ResponseBean taNoticeAdd(@RequestBody TaNotice taNotice) {
        ResponseBean responseBean = new ResponseBean();
        try {
            taNotice.setCreateDate(LocalDateTime.now());
            if (iTaNoticeService.save(taNotice)) {
                responseBean.addSuccess(taNotice);
            } else {
                responseBean.addError("fail");
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("taNoticeAdd -=- {}", e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 批量删除开屏通知
     *
     * @param taNoticeList
     */
    @ResponseBody
    @RequestMapping(value = "/channel/taNotice/batchDelete", method = RequestMethod.PUT)
    public ResponseBean batchDelete(@RequestBody List<TaNotice> taNoticeList) {
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean = iTaNoticeService.batchDelete(taNoticeList);
            logger.info("TaNoticeController.taNoticeDelete 返回结果：" + JSONObject.toJSONString(responseBean));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("taNoticeDelete -=- {}", e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

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

    /**
     * 根据id查询对象
     *
     * @param id 实体ID
     */
    @RequestMapping(value = "/channel/taNotice/{id}", method = RequestMethod.GET)
    public ResponseBean taNoticeGet(@PathVariable Integer id) {
        ResponseBean responseBean = new ResponseBean();
        try {
            TaNotice taNotice = iTaNoticeService.getById(id);
            if (taNotice == null) {
                responseBean.addError("fail");
                return responseBean;
            }
            if (taNotice.getTargetId() != null) {
                TaH5Sample taH5Sample = iTaH5SampleService.getById(taNotice.getTargetId());
                if (taH5Sample != null) {
                    taNotice.setTargetName(taH5Sample.getSampleName());
                }
            }
            responseBean.addSuccess(taNotice);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("taNoticeDelete -=- {}", e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 条件查询通知列表-admin
     *
     * @param pageNum
     * @param pageSize
     * @param type     类型 目前默认 sample
     * @return
     */
    @RequestMapping(value = "/admin/listNoticeByCondition", method = RequestMethod.GET)
    public ResponseBean listNoticeByConditionForAdmin(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                      @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                      String type) {
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean = iTaNoticeService.listNoticeByConditionForAdmin(pageNum, pageSize, type);
            logger.info("TaNoticeController.listNoticeByConditionForAdmin 返回结果：" + JSONObject.toJSONString(responseBean));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("listNoticeByConditionForAdmin -=- {}", e);
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }
}
