package com.supplier.controller;

import com.supplier.jwt.JWTUtil;
import com.supplier.model.Supplier;
import com.supplier.service.PersonalServce;
import com.supplier.util.DesDecry;
import com.supplier.util.DesEncry;
import com.supplier.util.ParsingToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 个人信息页面
 */
@RestController
@RequestMapping("personal")
public class PersonalController {

    @Autowired
    private PersonalServce personalServce;

    /**
     * 查询个人信息
     * @param request 获取头信息
     * @return 返回结果
     */
    @GetMapping("getPresonalMessage")
    public Map<String, Object> getPresonalMessage(HttpServletRequest request){
        Map<String, Object> map = new HashMap<>();
        try {
            //获取token
            Map<String, Object> userMap = ParsingToken.tokenParsing(request);
            String code = (String) userMap.get("code");
            Supplier supplier = personalServce.getPresonalMessage(code);
            supplier.setPhone(DesDecry.decrypt(supplier.getPhone()));
            map.put("msg", "200");
            map.put("data", supplier);
            map.put("payments", personalServce.balanceOfPayments(code));
        } catch (Exception e){
            e.printStackTrace();
            map.put("msg", "500");
        }
        return map;
    }

    /**
     * 修改手机号
     * @param supplier 参数
     * @param request 获取头部信息
     * @return 返回结果
     */
    @PutMapping("updatePhone")
    public Map<String, Object> updatePhone(Supplier supplier,HttpServletRequest request){
        Map<String, Object> map = new HashMap<>();
        try{
            //获取token
            Map<String, Object> userMap = ParsingToken.tokenParsing(request);
            supplier.setCode((String) userMap.get("code"));
            supplier.setPhone(DesEncry.encrypt(supplier.getPhone()));
            int count = personalServce.checkCaptcha(supplier);//校验验证码是否正确
            if(count > 0){
                //生成token传参
                Map<String, String> payload = new HashMap<String, String>(){{put("phone", supplier.getPhone());put("code", supplier.getCode());}};
                // 生成jwt的令牌
                String token = JWTUtil.getToken(payload);
                personalServce.updatePhone(supplier);
                map.put("data", token);
                map.put("msg", "200");
            } else {
                map.put("errMsg", "手机号或验证码不正确");
                map.put("msg", "300");
            }
        } catch (Exception e){
            e.printStackTrace();
            map.put("msg", "500");
        }
        return map;
    }

    /**
     * 修改昵称
     * @param supplier 参数
     * @param request 请求头参数
     * @return 返回结果
     */
    @PutMapping("updateName")
    public Map<String, Object> updateName(Supplier supplier,HttpServletRequest request){
        Map<String, Object> map = new HashMap<>();
        try {
            //获取token
            Map<String, Object> userMap = ParsingToken.tokenParsing(request);
            supplier.setCode((String) userMap.get("code"));

            personalServce.updatePhone(supplier);
            map.put("msg", "200");
        } catch (Exception e){
            e.printStackTrace();
            map.put("msg", "500");
        }
        return map;
    }

    /**
     * 查询报价单单价
     * @return
     */
    @GetMapping("getByNumber")
    public Map<String, Object> getByNumber(){
        return personalServce.getByNumber();
    }

    /**
     * 报价单次数购买余额支付
     * @param offerNum 参数
     * @return 返回结果
     */
    @GetMapping("payBlance")
    public Map<String, Object> payBlance(String offerNum, HttpServletRequest request){
        //获取token
        Map<String, Object> userMap = ParsingToken.tokenParsing(request);
        String code = (String) userMap.get("code");
        return personalServce.payBlance(offerNum, code);
    }

    /**
     * 收支明细
     * @param request 参数
     * @return 返回结果
     */
    @GetMapping("balanceOfPayments")
    public Map<String, Object> balanceOfPayments(HttpServletRequest request){
        String code = (String) ParsingToken.tokenParsing(request).get("code");
        return personalServce.balanceOfPayments(code);
    }

    /**
     * 查询充值次数
     * @param request 参数
     * @return 返回结果
     */
    @GetMapping("topUpNum")
    public Map<String, Object> topUpNum(HttpServletRequest request){
        String code = (String) ParsingToken.tokenParsing(request).get("code");
        return personalServce.topUpNum(code);
    }
}
