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.entity.HelpRecord;
import com.huiju.estateagents.entity.TaPerson;
import com.huiju.estateagents.service.IHelpRecordService;
import com.huiju.estateagents.service.ITaPersonService;
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-10-17
 */
@RestController
@RequestMapping("/api")
public class HelpRecordController extends BaseController {

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

    @Autowired
    public IHelpRecordService taHelpRecordService;
    
    @Autowired
    private ITaPersonService taPersonService;


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

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

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

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

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

    /**
     * 根据id查询对象
     * @param id  实体ID
     */
    @RequestMapping(value="/helpRecord/{id}",method= RequestMethod.GET)
    public ResponseBean helpRecordGet(@PathVariable Integer id){
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean.addSuccess(taHelpRecordService.getById(id));
        }catch (Exception e){
            e.printStackTrace();
            logger.error("helpRecordDelete -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }
    
    
    /**
     * 微信端助力者助力
     * @param helpRecord 实体对象
     * @return
     */
    @RequestMapping(value="/wx/helpRecord",method= RequestMethod.POST)
    public ResponseBean wxHelpRecordAdd(@RequestBody HelpRecord helpRecord, 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 {
            responseBean = taHelpRecordService.saveBooster(helpRecord,orgId,person);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("helpRecordAdd -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }
}
