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.TsCustomerGenderStatistic;
import com.huiju.estateagents.statistic.service.ITsCustomerGenderStatisticService;
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 TsCustomerGenderStatisticController extends BaseController {

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

    @Autowired
    public ITsCustomerGenderStatisticService iTaCustomerGenderStatisticService;
    
    @Autowired
    private ITaPersonService taPersonService;


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

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

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

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

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

    /**
     * 根据id查询对象
     * @param id  实体ID
     */
    @RequestMapping(value="/taCustomerGenderStatistic/{id}",method= RequestMethod.GET)
    public ResponseBean taCustomerGenderStatisticGet(@PathVariable Integer id){
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean.addSuccess(iTaCustomerGenderStatisticService.getById(id));
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taCustomerGenderStatisticDelete -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }
    
    /**
     * 微信端查询三条性别列表数据
     * @return
     */
    @RequestMapping(value="/wx/genderStatistic/{customerType}",method= RequestMethod.GET)
    public ResponseBean wxCustomerGenderStatisticList(@PathVariable(value = "customerType") String customerType, HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            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);
            //查询当前小程序的当前置业顾问男女比统计
            QueryWrapper<TsCustomerGenderStatistic> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("org_id",getOrgId(request));
            queryWrapper.eq("customer_type",customerType);
            queryWrapper.eq("realty_consultant",person.getPersonId());
            queryWrapper.orderByDesc("create_date");
     
            List<TsCustomerGenderStatistic> result = iTaCustomerGenderStatisticService.list(queryWrapper);
            responseBean.addSuccess(result);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taCustomerGenderStatisticList -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }
    
}
