package com.cool.bang.modules.api.controller;

import com.cool.bang.common.base.BaseController;
import com.cool.bang.common.constant.ControllerConstant;
import com.cool.bang.common.util.Client;
import com.cool.bang.common.util.WeixinUtil;
import com.cool.bang.common.web.response.Result;
import com.cool.bang.modules.api.domain.CorpVo;
import com.cool.bang.modules.business.domain.Corp;
import com.cool.bang.modules.business.service.ICorpService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 客源
 *
 * @author Sy
 * @version 1.0.0
 * 2024-10-21 18:05
 */
@RestController
@RequestMapping(ControllerConstant.WX + "login/v1")
public class WxlgoinController extends BaseController {

    @Value("${wx.appid}")
    private String appid;

    @Value("${wx.secret}")
    private String secret;

    @Value("${corp.url}")
    private String corpUrl;

    @Autowired
    private ICorpService corpService;


    /**
     * 获取用户OpenID，登录首页
     *
     * @return
     */
    @GetMapping("getOpenId")
    public Result getOpenId(String code) {
        //通过code换取网页授权access_token的地址
        String url = "https://api.weixin.qq.com/sns/oauth2/access_token?";
        url += "appid=" + appid;
        url += "&secret=" + secret;
        url += "&code=" + code;
        url += "&grant_type=authorization_code";
        String result = WeixinUtil.getResult(url);
        System.out.println(result);
        String openid = JSONObject.fromObject(result).getString("openid");
        Map<String, String> res = new HashMap<>();
        res.put("openid",openid);
        return success("查询成功",res);
    }


    /**
     * 根据OpenID获取用户，登录首页
     *
     * @return
     */
    @GetMapping("getUser")
    public Result getUser(String openid) {
        //根据openid获取用户信息
        Corp wxUser = corpService.queryByOpenid(openid);
        Map<String, Object> res = new HashMap<>();
        //examineStatus: 0未认证，1正在审核，2审核通过
        res.put("openId",openid);
        res.put("examineStatus",0);
        //如果没有微信用户信息
        if (wxUser == null) {
            return success("暂无用户信息",res);
        } else if(wxUser.getExamineStatus() == 2) {
            res.put("id",wxUser.getId());
            res.put("examineStatus",3);
            return  success("用户审批不通过,审批意见："+wxUser.getExamineRemark(),res);
        } else {
            res.put("name",wxUser.getName());
            res.put("id",wxUser.getId());
            res.put("examineStatus",1);
            res.put("district",wxUser.getDistrict());
            res.put("districtName",wxUser.getDistrictName());
            if (wxUser.getExamineStatus() == 1){
                res.put("examineStatus",2);
            }
            return success(res);
        }
    }

    /**
     * 注册
     *
     * @return
     */
    @PostMapping("add")
    public Result add(@RequestBody Corp corp) {
        Map<String, String> map = new HashMap<>();
        map.put("shtyxydm",corp.getIdCard());
        String json = Client.getMap(corpUrl+"/organization/queryOrgInfo",new HashMap<>(),map);
        //测试接口
        //String json = Client.getMap(corpUrl+"/manager/api/organization/queryOrgInfo",new HashMap<>(),map);
        System.out.println("接口返回："+json);
        com.alibaba.fastjson.JSONObject jo = com.alibaba.fastjson.JSONObject.parseObject(json);
        if (jo.getBoolean("code") != true){
            return failure("查询中介机构企业为空，请检查社会信用代码或联系管理员！");
        }
        CorpVo res = jo.getObject("data", CorpVo.class);
        if (res == null) {
            return failure("查询中介机构企业为空，请检查社会信用代码或联系管理员！");
        }
        corp.getCorpView(res);
        Corp wxUser = corpService.queryByIdCard(corp.getIdCard());
        if (wxUser != null){
            corp.setId(wxUser.getId());
            corpService.update(corp);
            return success(corp);
        }
        corp.setCreateTime(new Date());
        corpService.save(corp);
        return success(corp);
    }


    /**
     * 驳回修改-修改企业
     *
     * @return
     */
    @PostMapping("corpEdit")
    public Result corpEdit(@RequestBody Corp corp) {
        corp.setUpdateTime(new Date());
        corp.setExamineStatus(0);
        return decide(corpService.update(corp));
    }
}
