package com.huiju.estateagents.controller;

import com.alibaba.fastjson.JSONObject;
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 io.swagger.annotations.ApiOperation;
import com.huiju.estateagents.common.JWTUtils;
import com.huiju.estateagents.common.StringUtils;
import com.huiju.estateagents.entity.TaChannel;
import com.huiju.estateagents.entity.TaPerson;
import com.huiju.estateagents.mapper.TaChannelMapper;
import com.huiju.estateagents.service.ITaPersonService;
import com.huiju.estateagents.service.TaChannelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.apache.http.HttpRequest;
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;


/**
 * <p>
    *   前端控制器
    * </p>
 *
 * @author jobob
 * @since 2019-09-17
 */
@RestController
@RequestMapping("/api")
@Api(value = "渠道管理", tags = "渠道管理")
public class TaChannelController extends BaseController {

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

    @Autowired
    public TaChannelService taChannelService;

    @Autowired
    public ITaPersonService taPersonService;

    @Autowired
    public TaChannelMapper taChannelMapper;


    /**
     * 渠道管理列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "渠道管理列表", notes = "渠道管理列表")
    @ApiImplicitParams({
            @ApiImplicitParam(dataTypeClass = Integer.class, name = "pageNum", paramType = "query",value = "第几页"),
            @ApiImplicitParam(dataTypeClass = Integer.class, name = "pageSize", paramType = "query",value = "一页多少行"),
            @ApiImplicitParam(dataTypeClass = String.class, name = "channelId", paramType = "query",value = "渠道Id"),
    })
    @RequestMapping(value="/admin/channel",method= RequestMethod.GET)
    public ResponseBean channelList(@RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                    @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize,
                                    @RequestParam(value ="channelId",required = false) Integer channelId,
                                    HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        Integer orgId = getOrgId(request);
        try {
            //使用分页插件
		    IPage<TaChannel> pg = new Page<>(pageNum, pageSize);
            IPage<TaChannel> result = taChannelMapper.pageTaChannel(pg,orgId,channelId);
            HashMap hashMap  = new HashMap<>();
            hashMap.put("result",result);
            // 下拉列表的数据 all
            List<TaChannel> taChannelList = taChannelMapper.selectChannelList(orgId);
            hashMap.put("channelNmae",taChannelList);
            responseBean.addSuccess(hashMap);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("channelList -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 渠道保存
     * @param channel 实体对象
     * @return
     */
    @ApiOperation(value = "渠道保存", notes = "渠道保存")
    @ApiImplicitParams({
            @ApiImplicitParam(dataTypeClass = TaChannel.class, name = "channel", paramType = "body",value = "当前对象"),
    })
    @RequestMapping(value="/admin/channel",method= RequestMethod.POST)
    public ResponseBean channelAdd(@RequestBody TaChannel channel ,HttpServletRequest request){
        Integer orgId = getOrgId(request);
        ResponseBean responseBean = new ResponseBean();
        try {
            // 生成6位数的渠道码
            int channelCode= taChannelMapper.channelCode();
            channel.setChannelCode(String.valueOf(channelCode));
            channel.setOrgId(orgId);
            if (taChannelService.save(channel)){
                responseBean.addSuccess(channel);
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("channelAdd -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 根据id删除对象
     * @param id  实体ID
     */
    @ApiOperation(value = "根据渠道ID删除", notes = "根据渠道ID删除")
    @ApiImplicitParams({
            @ApiImplicitParam(dataTypeClass = Integer.class, name = "id", paramType = "query",value = "渠道id"),
    })
    @ResponseBody
    @RequestMapping(value="/channel/{id}", method= RequestMethod.DELETE)
    public ResponseBean channelDelete(@PathVariable Integer id){
        ResponseBean responseBean = new ResponseBean();
        try {
            if(taChannelService.removeById(id)){
                responseBean.addSuccess("success");
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("channelDelete -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 修改对象
     * @param id  实体ID
     * @param channel 实体对象
     * @return
     */
    @ApiOperation(value = "根据渠道ID修改", notes = "根据渠道ID修改")
    @ApiImplicitParams({
            @ApiImplicitParam(dataTypeClass = Integer.class, name = "id", paramType = "query",value = "渠道id"),
    })
    @RequestMapping(value="/admin/channel/{id}",method= RequestMethod.PUT)
    public ResponseBean channelUpdate(@PathVariable Integer id,
                                        @RequestBody TaChannel channel){
        ResponseBean responseBean = new ResponseBean();
        try {
            channel.setChannelId(id);
            if (taChannelService.updateById(channel)){
                responseBean.addSuccess(channel);
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("channelUpdate -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 根据id查询对象
     * @param id  实体ID
     */
    @ApiOperation(value = "根据渠道ID查询", notes = "根据渠道ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(dataTypeClass = Integer.class, name = "id", paramType = "query",value = "渠道id"),
    })
    @RequestMapping(value="/admin/channel/{id}",method= RequestMethod.GET)
    public ResponseBean channelGet(@PathVariable Integer id){
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean.addSuccess(taChannelService.getById(id));
        }catch (Exception e){
            e.printStackTrace();
            logger.error("channelDelete -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 渠道管理下的经纪人
     */
    @ApiOperation(value = "渠道管理下的经纪人", notes = "渠道管理下的经纪人")
    @ApiImplicitParams({
            @ApiImplicitParam(dataTypeClass = String.class, name = "name", paramType = "query",value = "姓名"),
            @ApiImplicitParam(dataTypeClass = String.class, name = "phone", paramType = "query",value = "电话"),
            @ApiImplicitParam(dataTypeClass = Integer.class, name = "channelId", paramType = "query",value = "渠道id"),
            @ApiImplicitParam(dataTypeClass = Integer.class, name = "pageNum", paramType = "query",value = "多少页"),
            @ApiImplicitParam(dataTypeClass = Integer.class, name = "pageSize", paramType = "query",value = "每页多少条"),
    })
    @RequestMapping(value="/admin/channel/broker",method= RequestMethod.GET)
    public  ResponseBean channelBrokerList(@RequestParam(value="name",required = false) String name,
                                           @RequestParam(value="phone",required = false) String phone,
                                           @RequestParam(value="channelId",required = false) Integer channelId,
                                           @RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                           @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize,
                                           HttpServletRequest request){
        Integer orgid = getOrgId(request);
        ResponseBean taPerson =  taPersonService.channelBrokerList(name,phone,pageNum,pageSize,orgid,channelId);
        return taPerson;
    }

    /**
     * 渠道邀请经纪人列表
     */
    @ApiOperation(value = "渠道邀请经纪人列表", notes = "渠道邀请经纪人列表")
    @ApiImplicitParams({
            @ApiImplicitParam(dataTypeClass = String.class, name = "id", paramType = "query",value = "姓名"),
            @ApiImplicitParam(dataTypeClass = String.class, name = "phone", paramType = "query",value = "电话"),
            @ApiImplicitParam(dataTypeClass = Integer.class, name = "channelId", paramType = "query",value = "渠道id"),
            @ApiImplicitParam(dataTypeClass = Integer.class, name = "pageNum", paramType = "query",value = "多少页"),
            @ApiImplicitParam(dataTypeClass = Integer.class, name = "pageSize", paramType = "query",value = "每页多少条"),
    })
    @RequestMapping(value="/admin/channel/InviteClientsList",method= RequestMethod.GET)
    public  ResponseBean InviteClientsList(@RequestParam(value="id",required = false) String id,
                                           @RequestParam(value="phone",required = false) String phone,
                                           @RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                           @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize,
                                           HttpServletRequest request){
        ResponseBean taPerson =  taPersonService.InviteClientsList(id,phone,pageNum,pageSize);
        return taPerson;
    }
}
