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.TaCustomerFollowUpRecord;
import com.huiju.estateagents.statistic.service.ITaCustomerFollowUpRecordService;
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.time.LocalDateTime;
import java.util.List;

/**
 * <p>
    * 跟进记录表 小程序盘客工具我的客户跟进记录信息 前端控制器
    * </p>
 *
 * @author jobob
 * @since 2019-11-08
 */
@RestController
@RequestMapping("/api")
public class TaCustomerFollowUpRecordController extends BaseController {

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

    @Autowired
    public ITaCustomerFollowUpRecordService iTaCustomerFollowUpRecordService;
    
    @Autowired
    private ITaPersonService taPersonService;

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

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

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

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

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

    /**
     * 根据id查询对象
     * @param id  实体ID
     */
    @RequestMapping(value="/taCustomerFollowUpRecord/{id}",method= RequestMethod.GET)
    public ResponseBean taCustomerFollowUpRecordGet(@PathVariable Integer id){
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean.addSuccess(iTaCustomerFollowUpRecordService.getById(id));
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taCustomerFollowUpRecordDelete -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }
    
    /**
     * 盘客工具添加跟进记录
     * @param taCustomerFollowUpRecord 实体对象
     * @return
     */
    @RequestMapping(value="/wx/taCustomerFollowUpRecord",method= RequestMethod.POST)
    public ResponseBean wxCustomerFollowUpRecordAdd(@RequestBody TaCustomerFollowUpRecord taCustomerFollowUpRecord, HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        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);
        try {
            taCustomerFollowUpRecord.setFollowerId(person.getPersonId());
            taCustomerFollowUpRecord.setCreateDate(LocalDateTime.now());
            taCustomerFollowUpRecord.setOrgId(orgId);
            if (iTaCustomerFollowUpRecordService.save(taCustomerFollowUpRecord)){
                responseBean.addSuccess(taCustomerFollowUpRecord);
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taCustomerFollowUpRecordAdd -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }
    
    /**
     * 分页查询跟进记录列表
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @RequestMapping(value="/wx/taCustomerFollowUpRecord",method= RequestMethod.GET)
    public ResponseBean wxCustomerFollowUpRecordList(@RequestParam(value ="pageNumber",defaultValue = "1") Integer pageNumber,
                                                     @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize,
                                                     @RequestParam(value ="customerId",required = true) String customerId,
                                                     HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        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);
        try {
            //使用分页插件
            IPage<TaCustomerFollowUpRecord> pg = new Page<>(pageNumber, pageSize);
            QueryWrapper<TaCustomerFollowUpRecord> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("follower_id",person.getPersonId());
            queryWrapper.eq("org_id",orgId);
            queryWrapper.eq("customer_id",customerId);
            queryWrapper.orderByDesc("create_date");
            
            IPage<TaCustomerFollowUpRecord> result = iTaCustomerFollowUpRecordService.page(pg, queryWrapper);
            responseBean.addSuccess(result);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taCustomerFollowUpRecordList -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }
}

