package com.huiju.estateagents.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.huiju.estateagents.base.BaseController;
import com.huiju.estateagents.base.ResponseBean;
import com.huiju.estateagents.common.CommConstant;
import com.huiju.estateagents.entity.*;
import com.huiju.estateagents.service.ITaSharePersonFromService;
import com.huiju.estateagents.service.ITdLiveDictService;
import org.apache.commons.lang3.StringUtils;
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;
import java.util.List;

/**
 * <p>
    *   前端控制器
    * </p>
 *
 * @author jobob
 * @since 2019-10-08
 */
@RestController
@RequestMapping("/")
public class TaSharePersonFromController extends BaseController {

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

    @Autowired
    public ITaSharePersonFromService iTaSharePersonFromService;

    @Autowired
    public ITdLiveDictService tdLiveDictService;


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

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

    /**
     * 保存对象--分享记录埋点
     *
     * @return
     */
    @RequestMapping(value="/wx/taSharePersonFrom",method= RequestMethod.POST)
    public ResponseBean taSharePersonFromAdd( @RequestParam(required = false) String from,
                                              @RequestParam(required = false)String recommender,
                                              @RequestParam(required = false)String targetId,
                                              HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            if(StringUtils.isNotBlank(from) &&StringUtils.isNotBlank(recommender) &&StringUtils.isNotBlank(targetId)){
                //添加客户分享信息记录
                TaSharePersonFrom taSharePersonFrom = iTaSharePersonFromService.addSharePersonRecord(getPersonId(request), getOrgId(request), from, recommender, targetId);
                responseBean.addSuccess(taSharePersonFrom);
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taSharePersonFromAdd -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

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

    /**
     * 根据id查询对象
     */
    @RequestMapping(value="api/admin/taLiveVisit",method= RequestMethod.GET)
    public ResponseBean taLiveActivityVisit(@RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                               @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize,
                                               @RequestParam(value = "sceneType", required = false)String sceneType,
                                               @RequestParam(value = "personType", required = false)String personType,
                                               @RequestParam(value = "phone", required = false)String phone,
                                               @RequestParam(value = "liveActivityId")String liveActivityId, HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            if (liveActivityId == null){
                return ResponseBean.success("");
            }
            responseBean = iTaSharePersonFromService.selectLiveActivityVisitList(pageNum, pageSize, getOrgId(request), personType, sceneType, phone, liveActivityId);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taShareCountList -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

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

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

    /**
     * 根据id查询对象
     */
    @RequestMapping(value="api/admin/taHouseVisit",method= RequestMethod.GET)
    public ResponseBean taSharePersonFromVisit(@RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                               @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize,
                                               @RequestParam(value = "sceneType", required = false)String sceneType,
                                               @RequestParam(value = "personType", required = false)String personType,
                                               @RequestParam(value = "phone", required = false)String phone,
                                               @RequestParam(value = "salesBatchId")String salesBatchId, HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            if (salesBatchId == null){
                return ResponseBean.success("");
            }
            responseBean = iTaSharePersonFromService.selectHouseVisitList(pageNum, pageSize, getOrgId(request), personType, sceneType, phone, salesBatchId);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taShareCountList -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 查询直播平台列表
     * @param pageNum
     * @param pageSize
     * @param request
     * @return
     */
    @RequestMapping(value="api/admin/livePlatList",method= RequestMethod.GET)
    public ResponseBean taLivePlatList (@RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                        @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize, HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            QueryWrapper<TdLiveDict> tdLiveDictQueryWrapper = new QueryWrapper<>();
            tdLiveDictQueryWrapper.eq("status", 1);
            tdLiveDictQueryWrapper.orderByDesc("sort");
            List<TdLiveDict> tdLiveDictList = tdLiveDictService.list(tdLiveDictQueryWrapper);
            responseBean.addSuccess(tdLiveDictList);
        }catch (Exception e){
            e.printStackTrace();
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }
}
