package com.huiju.estateagents.statistic.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.StringUtils;
import com.huiju.estateagents.statistic.entity.TsUserBehaviorStatistic;
import com.huiju.estateagents.statistic.service.ITsUserBehaviorStatisticService;
import io.swagger.models.auth.In;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

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

/**
 * <p>
    * 用户行为统计表  前端控制器
    * </p>
 *
 * @author jobob
 * @since 2020-01-13
 */
@RestController
@RequestMapping("/api")
public class TsUserBehaviorStatisticController extends BaseController {

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

    @Autowired
    public ITsUserBehaviorStatisticService iTsUserBehaviorStatisticService;


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

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

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

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

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

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

    /**
     * 用户行为
     * @return
     */
    @GetMapping(value = "/admin/tsUserBehavior")
    public ResponseBean selectUserBehavior(
                                           @RequestParam(value = "startDate", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDate startDate,
                                           @RequestParam(value = "endDate", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)  LocalDate endDate,
                                           @RequestParam(value = "buildingId", required = false) String buildingId,
                                           @RequestParam(value = "org", required = false) String org,
                                           HttpServletRequest request) {
        Integer orgId = StringUtils.isEmpty(org) ? getOrgId(request) : Integer.valueOf(org);
        return iTsUserBehaviorStatisticService.selectUserBehavior(orgId, startDate, endDate, buildingId);
    }
}
