package com.wgb.system.service.service;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wgb.system.service.component.SmsUtil;
import com.wgb.system.service.domain.mapper.RegionMapper;
import com.wgb.system.service.domain.mapper.SysMapper;
import com.wgb.system.service.domain.mapper.SysSmsMapper;
import com.wgb.system.service.domain.model.Menu;
import com.wgb.system.service.domain.model.SysSms;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.time.LocalDate;
import java.util.List;

@Service
public class SysService extends ServiceBase{

    private final SysMapper sysMapper;
    private final RegionMapper regionMapper;
    private final SysSmsMapper sysSmsMapper;
    private final RedisTemplate<String,Object> redisTemplate;
    @Value("${redis.qy.layer.val}")
    private String layer;

    @Value("${redis.sms.time.out}")
    private long smsTimeOut;

    @Value("${sys.img.file}")
    private String imageFile;

    @Value("${sys.img.url}")
    private String imageUrl;

    @Autowired
    public SysService(SysMapper sysMapper,RegionMapper regionMapper,SysSmsMapper sysSmsMapper,RedisTemplate<String,Object> redisTemplate) {
        this.sysMapper = sysMapper;
        this.regionMapper=regionMapper;
        this.sysSmsMapper=sysSmsMapper;
        this.redisTemplate=redisTemplate;
    }


    public List<Menu> getMenuList(){
        return sysMapper.getMenuList();
    }


    public String sendBusinessSms(String userCellphone,String smsType)throws Exception{
        String code=RandomStringUtils.random(6, false, true);
        SendSmsResponse response= SmsUtil.sendSmsBound(userCellphone,code);
        if (response.getCode().equalsIgnoreCase("OK")){
            sysSmsMapper.insert(new SysSms(userCellphone,smsType,code));
        }
        return code;
    }

    public String sendLoginCodeSms(String userCellphone,String smsType)throws Exception{
        String code=RandomStringUtils.random(6, false, true);
        SendSmsResponse response=SmsUtil.sendSmsCode(userCellphone,code);
        handlerSms(response);
        redisTemplate.opsForValue().set(layer+smsType+userCellphone,code,smsTimeOut);//设置订单超时为5分钟
        sysSmsMapper.insert(new SysSms(userCellphone,smsType,code));
        return code;
    }

    //验证验证码
    public void inspectSms(String userCellphone,String smsCode,String smsType){
        String redisCode=(String)redisTemplate.opsForValue().get(layer+smsType+userCellphone);
        handlerSmsCode(redisCode,smsCode);
        redisTemplate.delete(layer+smsType+userCellphone);//删除使用的验证码
        SysSms sysSms= sysSmsMapper.selectOne(new QueryWrapper<SysSms>().lambda().eq(SysSms::getUserCellphone,userCellphone).eq(SysSms::getCode,smsCode).eq(SysSms::getSmsType,smsType).eq(SysSms::getState,1));
        if (sysSms != null) {
            sysSms.setState(1);
            sysSmsMapper.updateById(sysSms);
        }
    }

    public String pushFile(MultipartFile file)throws Exception{
        if (file != null && !file.isEmpty()) {
            String prefix=file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")+1);
            String imageName=LocalDate.now().toString().replaceAll("-","")+RandomStringUtils.random(4, false, true)+"."+prefix;
            String picPath = imageFile +imageName;
            FileUtils.writeByteArrayToFile(new File(picPath), file.getBytes());
            return imageUrl+imageName;
        }else{
            handlerLicenceImage();
        }
        return "";
    }

}
