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.common.StringUtils;
import com.huiju.estateagents.entity.*;
import com.huiju.estateagents.service.ITaPersonFromRecordService;
import com.huiju.estateagents.service.ITaPersonService;
import com.huiju.estateagents.statistic.service.ITsUserBehaviorStatisticService;
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.List;

/**
 * <p>
    * 新增用户来源表  前端控制器
    * </p>
 *
 * @author jobob
 * @since 2020-01-03
 */
@RestController
@RequestMapping("/api")
public class TaPersonFromRecordController extends BaseController {

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

    @Autowired
    public ITaPersonFromRecordService iTaPersonFromRecordService;

    @Autowired
    public ITaPersonService taPersonService;

    @Autowired
    private ITsUserBehaviorStatisticService iTsUserBehaviorStatisticService;

    /**
     * 分页查询列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value="/admin/taPersonFromRecord",method= RequestMethod.GET)
    public ResponseBean taPersonFromRecordList(@RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                               @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize){
        ResponseBean responseBean = new ResponseBean();
        iTaPersonFromRecordService.personFromStatistic();
        iTsUserBehaviorStatisticService.userBehaviorStatustic();
        responseBean.addSuccess("成功");
        return responseBean;
    }

    /**
     * 保存对象
     * @param taPersonFromRecord 实体对象
     * @return
     */
    @RequestMapping(value="/taPersonFromRecord",method= RequestMethod.POST)
    public ResponseBean taPersonFromRecordAdd(@RequestBody TaPersonFromRecord taPersonFromRecord){
        ResponseBean responseBean = new ResponseBean();
        try {
            if (iTaPersonFromRecordService.save(taPersonFromRecord)){
                responseBean.addSuccess(taPersonFromRecord);
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taPersonFromRecordAdd -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

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

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

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

}
