package com.bohuikeji.delivery.backstage.controller.api.customer;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bohuikeji.delivery.backstage.entity.*;
import com.bohuikeji.delivery.backstage.entity.socket.AdminNotice;
import com.bohuikeji.delivery.backstage.service.*;
import com.bohuikeji.frame.module.admin.base.ApiController;
import com.bohuikeji.frame.module.admin.entity.back.APIEntity;
import com.bohuikeji.frame.module.admin.utils.NumberUtils;
import com.bohuikeji.frame.module.admin.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * @author zhouliwu
 * 招商加盟合作
 * 注：所以接口类必须继承 ApiController，否则无法被接口文档识别
 */
@Api(tags = {"客户端招商加盟合作"})
@RestController
@Slf4j
@RequestMapping("/api/v2/join")
public class InviteJoinV2Api extends ApiController {
    @Autowired
    ITCarouselService carouselService;
    @Autowired
    ITCarouselByteService carouselByteService;
    @Autowired
    ITCustomerService customerService;
    @Autowired
    ITCustomerBigService customerBigService;
    @Autowired
    ITInviteJoinService inviteJoinService;
    @Autowired
    ITCarouselExplainService carouselExplainService;
    @Autowired
    ISocketService socketService;
    @Autowired
    ITDiscountPayService discountPayService;
    @Autowired
    ITDiscountMallService discountMallService;

    /**
     * 招商加盟轮播
     * @param linkType
     * @return
     */
    @ApiOperation("招商加盟入驻轮播")
    @GetMapping("/get/list")
    public APIEntity getCarouselList(String linkType){
        List<TCarousel> carousels;
        if("APPLAT".equals(linkType)){
            carousels = carouselService.list(new QueryWrapper<TCarousel>().eq("program_type","全部"));//.notIn("link_type","外部链接")
        }else {
            carousels = carouselService.list(new QueryWrapper<TCarousel>().notIn("link_type","APPLAT"));
        }
        return APIEntity.success(carousels);
    }

    /**
     * 招商加盟入驻轮播(抖音小程序接口)
     * @return
     */
    @ApiOperation("招商加盟入驻轮播(抖音小程序接口)")
    @GetMapping("/get/douyin/list")
    public APIEntity getCarouselDouyinList(){
        List<TCarouselByte> carousels = carouselByteService.list(new QueryWrapper<TCarouselByte>().eq("program_type","全部"));
        return APIEntity.success(carousels);
    }

    /**
     * 招商加盟轮播
     * @param explainNum
     * @return
     */
    @ApiOperation("招商加盟入驻文字说明")
    @GetMapping("/get/explain")
    public APIEntity getCarouseExplain(Integer explainNum){
        TCarouselExplain tCarouselExplain = carouselExplainService.getOne(new QueryWrapper<TCarouselExplain>().eq("explain_num",explainNum));
        JSONObject json=new JSONObject();
        json.put("explainNum",tCarouselExplain.getExplainNum());
        json.put("funcRemark",tCarouselExplain.getFuncRemark());
        json.put("explainContent",tCarouselExplain.getExplainContent().replaceAll("。","\r\n"));
        return APIEntity.success(json);
    }

    /**
     * 商家入驻添加信息
     * @param id
     * @param doorPhoto
     * @param licensePhoto
     * @param idPhoto
     * @return
     */
    @ApiOperation("商家入驻添加信息")
    @GetMapping("/add/phono")
    @ApiImplicitParams({
            @ApiImplicitParam(name="doorPhoto",value="门头照",dataType="String", paramType = "query",example="",required=true),
            @ApiImplicitParam(name="licensePhoto",value="营业执照",dataType="String", paramType = "query",example="",required=true),
            @ApiImplicitParam(name="idPhoto",value="身份证反正面",dataType="String", paramType = "query",example="",required=true)})
    public APIEntity businessSettled(@ApiIgnore("id") @RequestAttribute("JwtUserId") Integer id,String doorPhoto,String licensePhoto, String idPhoto){
        /*TCustomer customer = customerService.getById(id);
        if(customer == null){
            return APIEntity.fail("用户未登录");
        }
        if(StringUtils.isNotBlank(customer.getTemplateId())){
            return APIEntity.fail("当前账户已绑定了商户与计费模板");
        }
        if(customerBigService.list(new QueryWrapper<TCustomerBig>().eq("customer_id",customer.getCustomerId()).notIn("big_status","已结束")).size()>0){
            return APIEntity.fail("您添加的商户已存在，不要重复添加");
        }
        if(StringUtils.isNotBlank(customer.getDoorPhoto()) && StringUtils.isNotBlank(customer.getLicensePhoto()) && StringUtils.isNotBlank(customer.getIdPhoto())){
            return APIEntity.fail("您已入驻成功，请不要重复申请！");
        }
        TCustomerBig customerBig = new TCustomerBig();
        customerBig.setBigPhone(customer.getCustomerPhone());
        customerBig.setBigName(customer.getCustomerName());
        customerBig.setBigServer("其他");
        customerBig.setBigNum(NumberUtils.getShort());
        customerBig.setCustomerId(customer.getCustomerId());
        customerBig.setStorePhoto(doorPhoto);
        customerBig.setBigLicense(licensePhoto);
        customerBig.setIdPhoto(idPhoto);
        customerBig.setBigStatus("审核中");*/
        return APIEntity.success("请到个人中心-商家认证添加商户信息");
    }

    /**
     * 招商加盟信息录入
     * @param joinName
     * @param joinPhone
     * @param corporateName
     * @param urbanArea
     * @param brandName
     * @param content
     * @param funcRemark
     * @return
     */
    @ApiOperation("招商加盟入驻信息录入")
    @PostMapping("/add/info")
    @ApiImplicitParams({
            @ApiImplicitParam(name="joinName",value="加入姓名",dataType="String", paramType = "query",example="张三"),
            @ApiImplicitParam(name="joinPhone",value="联系电话",dataType="String", paramType = "query",example="15661255895"),
            @ApiImplicitParam(name="corporateName",value="公司名称",dataType="String", paramType = "query",example="***物流有限公司"),
            @ApiImplicitParam(name="urbanArea",value="城市区域",dataType="String", paramType = "query",example="呼和浩特市新城区"),
            @ApiImplicitParam(name="brandName",value="品牌名称",dataType="String", paramType = "query",example="同城急送"),
            @ApiImplicitParam(name="content",value="合作内容（广告内容）",dataType="String", paramType = "query",example="******"),
            @ApiImplicitParam(name="funcRemark",value="功能备注",dataType="String", paramType = "query",example="搬家货运公司、城市合伙人、广告招商、广告招商、业务合作、赞助商合作",required=true)})
    public APIEntity join(String joinName,String joinPhone,String corporateName,String urbanArea,String brandName,String content,String funcRemark){
        String regex = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(16[5,6])|(17[0-8])|(18[0-9])|(19[1、5、8、9]))\\d{8}$";
        Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(joinPhone);
        if(m.matches() != true){
            return APIEntity.fail("您好，您的手机号码格式不正确！");
        }
        List<TInviteJoin> inviteJoins = inviteJoinService.list(new QueryWrapper<TInviteJoin>().eq("join_name",joinName).eq("join_phone",joinPhone).eq("func_remark",funcRemark));
        if(inviteJoins.size()>0){
            return APIEntity.fail("您好，您已添加成功，请勿重复添加");
        }
        if(inviteJoins.size()>0){
            return APIEntity.fail("您好，您已添加成功，请勿重复添加");
        }
        TInviteJoin inviteJoin = new TInviteJoin();
        inviteJoin.setJoinName(joinName);
        inviteJoin.setJoinPhone(joinPhone);
        inviteJoin.setCorporateName(corporateName);
        inviteJoin.setBrandName(brandName);
        inviteJoin.setUrbanArea(urbanArea);
        inviteJoin.setContent(content);
        inviteJoin.setFuncRemark(funcRemark);
        socketService.sendAdminMsg(new AdminNotice(StringUtils.formatStr("您好，有{0}的客户提交了{1}申请，请及时处理",urbanArea,funcRemark)));
        return APIEntity.success(inviteJoinService.save(inviteJoin));
    }

}
