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.web.response.Result;
import com.cool.bang.modules.business.domain.JobCustomer;
import com.cool.bang.modules.business.service.IJobCustomerService;
import com.cool.bang.modules.business.util.WeixinUtil;
import com.cool.bang.modules.system.service.ITokenService;
import com.cool.bang.modules.system.service.IUserService;
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.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * 客源
 *
 * @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;

    @Autowired
    private IJobCustomerService jobCustomerService;


    /**
     * 获取用户OpenID，登录首页
     *
     * @return
     */
    @GetMapping("getOpenId")
    public Result getOpenId(String code) {
        //通过code换取网页授权access_token的地址
        String url = "https://api.weixin.qq.com/sns/jscode2session?";
        url += "appid=" + appid;
        url += "&secret=" + secret;
        url += "&js_code=" + code;
        url += "&grant_type=authorization_code";
        String result = WeixinUtil.getResult(url);
        String openid = JSONObject.fromObject(result).getString("openid");
        //根据openid获取用户信息
        JobCustomer wxUser = jobCustomerService.queryByOpenid(openid);
        //如果没有微信用户信息
        if (wxUser == null) {
            return failure("暂无用户信息");
        } else {
            return success(wxUser);
        }
    }

    /**
     * 用户登录
     *
     * @return
     */
    @GetMapping("login")
    public Result login(String openid, String phone) {
        //根据openid获取用户信息
        JobCustomer wxUser = jobCustomerService.queryByOpenid(openid);
        //如果没有微信用户信息
        if (wxUser == null) {
            JobCustomer wxUser1 = jobCustomerService.queryByPhone(phone);
            if (wxUser1 == null) {
                wxUser1.setPhone(phone);
                wxUser1.setOpenid(openid);
                wxUser1.setName("匿名用户");
                wxUser1.setCreateDate(new Date());
                wxUser1.setDelFlag(1);
                jobCustomerService.save(wxUser1);
            }
            return success(wxUser1);
        } else {
            return success(wxUser);
        }
    }


    @PostMapping("add")
    public Result add(@Validated JobCustomer jobCustomer) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        String code = dtf.format(LocalDateTime.now());
        jobCustomer.setCustomerNumber(code);
        jobCustomer.setCreateDate(new Date());
        jobCustomer.setDelFlag(1);
        return decide(jobCustomerService.save(jobCustomer));
    }
}
