package com.yunzhi.marketing.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.yunzhi.marketing.base.BaseController;
import com.yunzhi.marketing.base.ResponseBean;
import com.yunzhi.marketing.entity.TaPerson;
import com.yunzhi.marketing.service.ITaPersonService;
import com.yunzhi.marketing.statistic.entity.TsCustomerStatisticMonthly;
import com.yunzhi.marketing.statistic.service.ITsCustomerStatisticMonthlyService;
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 TsCustomerStatisticMonthlyController extends BaseController {

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

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

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

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

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

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

    /**
     * 根据id查询对象
     * @param id  实体ID
     */
    @RequestMapping(value="/taCustomerStatisticMonthly/{id}",method= RequestMethod.GET)
    public ResponseBean taCustomerStatisticMonthlyGet(@PathVariable Integer id){
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean.addSuccess(iTaCustomerStatisticMonthlyService.getById(id));
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taCustomerStatisticMonthlyDelete -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }
    
    /**
     * 微信端查询月客户分析统计数据
     * 月筛选：可以选择包括当前月往前的12个月
     * @return
     */
    @RequestMapping(value="/wx/customerStatisticMonthly/{customerType}",method= RequestMethod.GET)
    public ResponseBean taCustomerStatisticMonthlyList(@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<TsCustomerStatisticMonthly> result = iTaCustomerStatisticMonthlyService.getCustomerMonthlyList(customerType, getOrgId(request),person.getPersonId(),person.getUserId());
            responseBean.addSuccess(result);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taCustomerStatisticMonthlyList -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }
}
