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.entity.TaPerson;
import com.huiju.estateagents.service.ITaPersonService;
import com.huiju.estateagents.statistic.entity.TsCustomerStatisticDaily;
import com.huiju.estateagents.statistic.service.ITsCustomerStatisticDailyService;
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.util.List;

/**
 * <p>
    * 客户分析日统计表 小程序盘客工具客户分析 前端控制器
    * </p>
 *
 * @author jobob
 * @since 2019-11-08
 */
@RestController
@RequestMapping("/api")
public class TsCustomerStatisticDailyController extends BaseController {

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

    @Autowired
    public ITsCustomerStatisticDailyService iTaCustomerStatisticDailyService;
    
    @Autowired
    private ITaPersonService taPersonService;

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

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

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

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

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

    /**
     * 根据id查询对象
     * @param id  实体ID
     */
    @RequestMapping(value="/taCustomerStatisticDaily/{id}",method= RequestMethod.GET)
    public ResponseBean taCustomerStatisticDailyGet(@PathVariable Integer id){
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean.addSuccess(iTaCustomerStatisticDailyService.getById(id));
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taCustomerStatisticDailyDelete -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }
    
    /**
     * 微信端查询日客户分析统计数据
     * 日筛选：可以选择包括当前日往前的31日
     * @return
     */
    @RequestMapping(value="/wx/customerStatisticDaily/{customerType}",method= RequestMethod.GET)
    public ResponseBean wxCustomerStatisticDailyList(@PathVariable(value = "customerType") String customerType,
                                                     HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        String openid = getOpenId(request);
        List<TaPerson> taPersons = taPersonService.getPersonsByOpenId(openid);
        if (null == taPersons || taPersons.size() != 1) {
            return ResponseBean.error("验证人员信息失败", ResponseBean.ERROR_UNAVAILABLE);
        }
        TaPerson person = taPersons.get(0);
        try {
            List<TsCustomerStatisticDaily> list = iTaCustomerStatisticDailyService.getDailyList(customerType,getOrgId(request),person.getPersonId(),person.getUserId());
            responseBean.addSuccess(list);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("wxCustomerStatisticDailyList -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }
}
