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.TaCustomerMessage;
import com.huiju.estateagents.entity.TaPerson;
import com.huiju.estateagents.service.ITaCustomerMessageService;
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-07-30
 */
@RestController
@RequestMapping("/api")
public class TaCustomerMessageController extends BaseController {

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

    @Autowired
    public ITaCustomerMessageService iTaCustomerMessageService;

    @Autowired
    private ITaPersonService taPersonService;

    /**
     * 分页查询列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value="/admin/taCustomerMessage",method= RequestMethod.GET)
    public ResponseBean taCustomerMessageList(@RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                              @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize,
                                              HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            //使用分页插件
		    IPage<TaCustomerMessage> pg = new Page<>(pageNum, pageSize);
            QueryWrapper<TaCustomerMessage> queryWrapper = new QueryWrapper<>();
            queryWrapper.orderByDesc("create_date");
            queryWrapper.eq("org_id", getOrgId(request));
            IPage<TaCustomerMessage> result = iTaCustomerMessageService.page(pg, queryWrapper);
            responseBean.addSuccess(result);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taCustomerMessageList -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 保存对象
     * @param taCustomerMessage 实体对象
     * @return
     */
    @RequestMapping(value="/taCustomerMessage",method= RequestMethod.POST)
    public ResponseBean taCustomerMessageAdd(@RequestBody TaCustomerMessage taCustomerMessage, HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            taCustomerMessage.setOrgId(getOrgId(request));
            if (iTaCustomerMessageService.save(taCustomerMessage)){
                responseBean.addSuccess(taCustomerMessage);
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taCustomerMessageAdd -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

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

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

    /**
     * 根据id查询对象
     * @param id  实体ID
     */
    @RequestMapping(value="/taCustomerMessage/{id}",method= RequestMethod.GET)
    public ResponseBean taCustomerMessageGet(@PathVariable Integer id){
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean.addSuccess(iTaCustomerMessageService.getById(id));
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taCustomerMessageDelete -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }
    
    /**
     * 保存对象
     * @param taCustomerMessage 实体对象
     * @return
     */
    @RequestMapping(value="/wx/taCustomerMessage",method= RequestMethod.POST)
    public ResponseBean wxCustomerMessageAdd(@RequestBody TaCustomerMessage taCustomerMessage, HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            String openid = getOpenId(request);
            TaPerson taPersons = taPersonService.getPersonsByOpenId(openid).get(0);
            taCustomerMessage.setStatus(CommConstant.STATUS_UNACCALIMED);
            taCustomerMessage.setCreateDate(LocalDateTime.now());
            taCustomerMessage.setPersonId(taPersons.getPersonId());
            taCustomerMessage.setNickname(taPersons.getNickname());
            taCustomerMessage.setOrgId(getOrgId(request));
            if (iTaCustomerMessageService.save(taCustomerMessage)){
                responseBean.addSuccess(taCustomerMessage);
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taCustomerMessageAdd -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }
}
