package com.yunzhi.marketing.eContract.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.yunzhi.marketing.base.BaseController;
import com.yunzhi.marketing.base.ResponseBean;
import com.yunzhi.marketing.eContract.entity.TaContractUser;
import com.yunzhi.marketing.eContract.service.ITaContractUserService;
import com.yunzhi.marketing.entity.TaPerson;
import com.yunzhi.marketing.entity.TaRaiseRecord;
import com.yunzhi.marketing.service.ITaPersonService;
import com.yunzhi.marketing.service.ITaRaiseRecordService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
    * 电子合同注册用户表  前端控制器
    * </p>
 *
 * @author fxf
 * @since 2020-03-26
 */
@RestController
@RequestMapping("/api")
public class TaContractUserController extends BaseController {

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

    @Autowired
    public ITaContractUserService iTaContractUserService;


    @Autowired
    public ITaPersonService taPersonService;

    @Autowired
    private ITaRaiseRecordService taRaiseRecordService;

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

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

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

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

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

    /**
     * 根据id查询对象
     * @param id  实体ID
     */
    @RequestMapping(value="/taContractUser/{id}",method= RequestMethod.GET)
    public ResponseBean taContractUserGet(@PathVariable Integer id){
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean.addSuccess(iTaContractUserService.getById(id));
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taContractUserDelete -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }



    /**
     * 给用户在法大大注册账号
     */
    @RequestMapping(value = "/wx/contract/user/add", method = RequestMethod.POST)
    public ResponseBean addCompanyFirstInfo(@RequestBody TaRaiseRecord taRaiseRecord, 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);
            TaContractUser taContractUser = iTaContractUserService.newFirstCustomerInfo(person,taRaiseRecordService.getById(taRaiseRecord.getRaiseRecordId()));
            responseBean.addSuccess(taContractUser);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("taCompanyList -=- {}", e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }


    /**
     * 获取个人用户的最新状态
     */
    @RequestMapping(value = "/wx/contract/user/get", method = RequestMethod.GET)
    public ResponseBean getCompanyFirstInfo(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);
            TaContractUser taContractUser = iTaContractUserService.getFirstCustomerInfo(person);
            responseBean.addSuccess(taContractUser);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("taCompanyList -=- {}", e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }
}
