package com.winsun.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.winsun.auth.core.common.model.ResponseData;
import com.winsun.bean.KdPhone;
import com.winsun.bean.Order;
import com.winsun.bean.Regular;
import com.winsun.bean.XshInventory;
import com.winsun.mapper.KdPhoneMapper;
import com.winsun.mapper.OrderMapper;
import com.winsun.mapper.RegularMapper;
import com.winsun.mapper.XshInventoryMapper;
import com.winsun.smsUtils.SendSmsAndMail;
import com.winsun.utils.RandomUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Author xiangli
 * @Date 2020/4/21
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/codesManager")
public class codeManagerController {
    @Autowired
    private RegularMapper regularMapper;
    @Autowired
    private KdPhoneMapper kdPhoneMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private XshInventoryMapper xshInventoryMapper;
    @Autowired
    @Qualifier("redisStringTemplate")
    private RedisTemplate redisTemplate;

    @ResponseBody
    @RequestMapping(value = "sendCodes", method = RequestMethod.POST)
    public ResponseData<Map<String, Object>> checkStudent(String phone) {

        Map<String, Object> map = new HashMap<>();
        try {
            Wrapper<KdPhone> wrapper1 = new EntityWrapper<>();
            wrapper1.setSqlSelect("id,phone,tcName");
            wrapper1.eq("phone",phone);
            List<KdPhone> list = kdPhoneMapper.selectList(wrapper1);
            Wrapper<Order> wrapper2 = new EntityWrapper<>();
            wrapper2.eq("user_type","0");
            wrapper2.eq("order_status","已完成");
            wrapper2.eq("business_number",phone);
            List<Order> list1 = orderMapper.selectList(wrapper2);
            if(list.size()>0 || list1.size()>0){
                // 生成6位随机数
                String veCode = String.valueOf((int) ((RandomUtil.getSecrityRandom() * 9 + 1) * 100000));
                //MessageUtil.sent(packageUpgrade.getAccNbr(),"4","您获取的验证码为："+s+",半小时内有效。");
                redisTemplate.opsForValue().set(phone, veCode, 240, TimeUnit.MINUTES);
                SendSmsAndMail.sendSms(phone,veCode + "","7");
                log.info("验证码:" + veCode);
                map.put("code",1);
            }else{
                map.put("code",0);
            }
        } catch (Exception e) {
            log.error("获取验证码异常:", e);
            map.put("code",2);
        }
        return ResponseData.success(map);
    }

    @ResponseBody
    @RequestMapping(value = "checkCodes", method = RequestMethod.POST)
    public ResponseData<Map<String, Object>> checkCodes(String phone, String codes) {
        if(!StringUtils.isNotEmpty(phone)){
            return ResponseData.error("手机号不正确！");
        }
        if(!StringUtils.isNotEmpty(codes)){
            return ResponseData.error("验证码不能为空！");
        }
        String ver = null;
        try {
            ver = redisTemplate.opsForValue().get(phone).toString();
        } catch (NullPointerException n) {
            return ResponseData.error("验证码已失效，请发送验证码！");
        }
        if(ver.equals(codes)){
            Map<String,Object> map =  new HashMap<>();
            Wrapper<KdPhone> kdPhoneWrapper = new EntityWrapper<>();
            kdPhoneWrapper.setSqlSelect("id,phone,tcName");
            kdPhoneWrapper.eq("phone",phone);
            List<KdPhone> list = kdPhoneMapper.selectList(kdPhoneWrapper);
            if(list.size()>0){
                KdPhone kdPhone = list.get(0);
                map.put("tcName",kdPhone.getTcName());
            }
            return ResponseData.success(map);
        }
        return ResponseData.error( "验证码错误" );
    }

    /**
     * @param regularId 正则id
     * @return
     */
    @RequestMapping(value = "getRegularById", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData<Object> getRegularById(String regularId) {

        Regular regulars = regularMapper.selectById(regularId);
        return ResponseData.success(regulars, "查询成功！");
    }
    //检查学生证号是否在学生清单内
    @ResponseBody
    @RequestMapping(value="queryStuNo", method = RequestMethod.POST)
    public ResponseData<Object> queryStuNo(String stuNumber){
        if(StringUtils.isEmpty(stuNumber)){
            return ResponseData.error("学生号不能为空");
        }
        XshInventory xshQd = xshInventoryMapper.selectById(stuNumber);
        if(xshQd == null){
            return ResponseData.error("学生号不在清单内！");
        }
        return ResponseData.success();
    }
}
