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.JWTUtils;
import com.huiju.estateagents.entity.TaPerson;
import com.huiju.estateagents.entity.TaPointsRecords;
import com.huiju.estateagents.service.ITaPersonService;
import com.huiju.estateagents.service.ITaPointsRecordsService;
import com.huiju.estateagents.service.ITaRecommendCustomerService;
import org.apache.ibatis.annotations.Mapper;
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.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
    * 积分消费流水表  前端控制器
    * </p>
 *
 * @author jobob
 * @since 2019-07-25
 */
@RestController
@RequestMapping("/api")
public class TaPointsRecordsController extends BaseController {

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

    @Autowired
    public ITaPointsRecordsService iTaPointsRecordsService;
    
    @Autowired
    private ITaPersonService taPersonService;

    @Autowired
    private ITaRecommendCustomerService recommendCustomerService;


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

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

    /**
     * 分页查询列表
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @RequestMapping(value="/wx/taPointsRecords",method= RequestMethod.GET)
    public ResponseBean taWxPointsRecordsList(@RequestParam(value ="pageNumber",defaultValue = "1") Integer pageNumber,
                                            @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize,
                                              HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            String openid = getOpenId(request);
            Integer orgId = getOrgId(request);
            List<TaPerson> taPersons = taPersonService.getPersonsByOpenId(openid);
            if (null == taPersons || taPersons.size() != 1) {
                return ResponseBean.error("验证人员信息失败", ResponseBean.ERROR_UNAVAILABLE);
            }
            TaPerson person = taPersons.get(0);
            //使用分页插件
            IPage<TaPointsRecords> pg = new Page<>(pageNumber, pageSize);
            QueryWrapper<TaPointsRecords> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("person_id",person.getPersonId());
            queryWrapper.orderByDesc("create_date");

            IPage<TaPointsRecords> result = iTaPointsRecordsService.page(pg, queryWrapper);
            for(int i = 0;i<result.getRecords().size();i++){
                switch (result.getRecords().get(i).getChangeType()){
                    case CommConstant.POINTS_CHANGE_GOODS:
                        result.getRecords().get(i).setRecordName("兑换商品");
                        break;
                    case CommConstant.POINTS_CHANGE_SHARE_POSTER:
                        result.getRecords().get(i).setRecordName("分享转发");
                        break;
                    case CommConstant.POINTS_CHANGE_CHECKIN:
                        result.getRecords().get(i).setRecordName("签到");
                        break;
                    case CommConstant.POINTS_CHANGE_RECOMMEND_CUSTOMER:
                        result.getRecords().get(i).setRecordName("推荐客户");
                        break;
                    case CommConstant.POINTS_CHANGE_SIGNUP_AGENT:
                        result.getRecords().get(i).setRecordName("授权手机号");
                        break;
                    case CommConstant.POINTS_CHANGE_GROUP:
                        result.getRecords().get(i).setRecordName("拼团");
                        break;
                    case CommConstant.POINTS_CHANGE_ACTIVITY_CHECKIN:
                        result.getRecords().get(i).setRecordName("活动签到");
                        break;
                    case CommConstant.POINTS_CHANGE_DOCUMENT_VERIFY:
                        result.getRecords().get(i).setRecordName("资料审核");
                        break;
                }
            }
            responseBean.addSuccess(result);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taPointsRecordsList -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

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

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

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

    /**
     * 根据id查询对象
     * @param id  实体ID
     */
    @RequestMapping(value="/admin/taPointsRecords/{id}",method= RequestMethod.GET)
    public ResponseBean taPointsRecordsGet(@PathVariable Integer id){
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean.addSuccess(iTaPointsRecordsService.getById(id));
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taPointsRecordsDelete -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }
    
//    /**
//     * 分页查询列表
//     * @param pageNum
//     * @param pageSize
//     * @return
//     */
//    @RequestMapping(value="/wx/taPointsRecords",method= RequestMethod.GET)
//    public ResponseBean wxPointsRecordsList(@RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
//                                            @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize,
//                                            HttpServletRequest request){
//        ResponseBean responseBean = new ResponseBean();
//        try {
//            String openid = getOpenId(request);
//            TaPerson taPerson = taPersonService.getPersonsByOpenId(openid).get(0);
//            //使用分页插件
//            IPage<TaPointsRecords> pg = new Page<>(pageNum, pageSize);
//            QueryWrapper<TaPointsRecords> queryWrapper = new QueryWrapper<>();
//            queryWrapper.eq("person_id",taPerson.getPersonId());
//            queryWrapper.orderByDesc("create_date");
//            IPage<TaPointsRecords> result = iTaPointsRecordsService.page(pg, queryWrapper);
//            responseBean.addSuccess(result);
//        }catch (Exception e){
//            e.printStackTrace();
//            logger.error("taPointsRecordsList -=- {}",e.toString());
//            responseBean.addError(e.getMessage());
//        }
//        return responseBean;
//    }

    /**
     * 分页查询列表
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @RequestMapping(value="/admin/mine/taPointsRecords/{id}",method= RequestMethod.GET)
    public ResponseBean CustomerPointsRecordsList(@RequestParam(value ="pageNumber",defaultValue = "1") Integer pageNumber,
                                            @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize,
                                            @PathVariable(value = "id") String id){
        ResponseBean responseBean = new ResponseBean();
        try {
            IPage<TaPointsRecords> pg = new Page<>(pageNumber, pageSize);
            QueryWrapper<TaPointsRecords> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("person_id",id);
            queryWrapper.orderByDesc("create_date");
            IPage<TaPointsRecords> result = iTaPointsRecordsService.page(pg, queryWrapper);
            Integer totalPoints = iTaPointsRecordsService.sumPointByPersonId(id);
            Map<String,Object> data = new HashMap<>();
            data.put("totalPoints",totalPoints);
            data.put("result",result);
            responseBean.addSuccess(data);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taPointsRecordsList -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }
}
