package com.winsun.controller;

import com.winsun.auth.core.base.controller.BaseController;
import com.winsun.auth.core.util.ResponseEntity;
import com.winsun.bean.SalesList;
import com.winsun.mapper.SalesListMapper;
import com.winsun.smsUtils.SendSmsAndMail;
import com.winsun.utils.MapUtil;
import com.winsun.utils.RandomUtil;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * 
 * @author dongyp
 *
 */
@Slf4j
@RestController
@RequestMapping("/mobile/salesList")
public class SalesListMobileController extends BaseController {
	
    // 登记验证码标识
    private static String CODE = "registerCode";
    //5分钟内有效
    private static long CODETIME = 1000 * 60*5;
    
	private SalesListMapper salesListMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
	
	public SalesListMobileController(SalesListMapper salesListMapper) {
		this.salesListMapper = salesListMapper;
	}
	
	@RequestMapping(value="/registerPhone", method = RequestMethod.POST)
	public Object registerPhone(@RequestParam("orderPhone") String orderPhone,@RequestParam("salesSubst") String salesSubst,
			@RequestParam("salesSchool") String salesSchool,@RequestParam("phoneCode") String phoneCode) {
		JSONObject jsonObject = new JSONObject();
		
    	// 处理放号清单号码的销售学校清单，用于重复判断
    	Map<String, Object> phoneSchoolMap = MapUtil.listToMap(salesListMapper.getPhoneSchool(), "order_phone", "school");
    	String desc = "";
    	boolean isRepeat = false;
    	String status = "1";
    	
    	
    	if(null != phoneSchoolMap.get(orderPhone)) {
			String phoneSchool = (String)phoneSchoolMap.get(orderPhone);
			
			if(phoneSchool.indexOf(salesSchool) > -1) {
				desc = "该放号号码已经在该销售学校登记";
				jsonObject.put("code", "200");
				jsonObject.put("desc", desc);
				
				return jsonObject;
			}else {
				desc = "该放号号码与其它学校有重复登记，请后期进行核对;";
				isRepeat = true;
				status = "0";
			}
		}
    	
    	SalesList order = new SalesList();
		order.setOrderPhone(orderPhone);
		order.setUserId("22");
		order.setCreateTime(new Date());
		order.setSalesSubst(salesSubst);
		order.setSalesSchool(salesSchool);
		order.setOrderType("1");
		order.setName("22");
		order.setAccount("22");
		int num = salesListMapper.insert(order);
		if(isRepeat) {
			salesListMapper.updateRepealOrder(status, desc, orderPhone);
		}
		
		return jsonObject;
	}
	
	
	@RequestMapping(value = "/getPhoneCode",method = RequestMethod.POST)
    public Object phoneLoginCodeY(@RequestParam("phone") String phone){

        //5分钟内有效
        String verificationCode = String.valueOf((int) ((RandomUtil.getSecrityRandom() * 9 + 1) * 100000));
        String code = phone + CODE;  
        Long expire = stringRedisTemplate.getExpire(code);
        //验证码 有效时间是五分钟倒计时
        if (expire>(60*4)){
            return ResponseEntity.newJSON("code", 400, "message", "请勿频繁发送手机验证码操作！");
        }
        stringRedisTemplate.opsForValue().set(code, verificationCode, CODETIME, TimeUnit.MILLISECONDS);
        Map<String, Object> sent=new HashMap<>();
        sent.put("code",400);
        try {

            if (phone.length() == 11){
                SendSmsAndMail.sendSms(phone, verificationCode, "7");
                sent.put("message", "发送成功");
                sent.put("code", 200);
            }else {
                sent.put("message","当前用户手机号码不合法！");
            }
            
        }catch (Exception e){
            log.info("错误信息：{}",e.getMessage());
            sent.put("message","手机短信发送失败");
        }

        return sent;
    }
}
