package com.wgb.system.service.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wgb.system.service.constant.SysConstant;
import com.wgb.system.service.domain.mapper.SysUserMapper;
import com.wgb.system.service.domain.mapper.UserMapper;
import com.wgb.system.service.domain.model.Region;
import com.wgb.system.service.domain.model.SysUser;
import com.wgb.system.service.domain.model.User;
import com.wgb.system.service.domain.request.LoginReqest;
import com.wgb.system.service.domain.request.UserReuqest;
import com.wgb.system.service.component.Md5Utls;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.util.StringUtil;
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 java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class AccessService extends ServiceBase{

    @Value("${redis.qy.layer.val}")
    private String layer;

    @Value("${redis.map.layer.val}")
    private String regionLayer;

    private SysUserMapper sysUserMapper;
    private UserMapper userMapper;
    private RedisTemplate<String,Object> redisTemplate;
    private SysService sysService;

    @Autowired
    public AccessService(SysUserMapper sysUserMapper,RedisTemplate<String,Object> redisTemplate,UserMapper userMapper,SysService sysService) {
        this.sysUserMapper = sysUserMapper;
        this.redisTemplate=redisTemplate;
        this.userMapper=userMapper;
        this.sysService=sysService;
    }

    public void getSysLogin(String username, String password)throws Exception{
        QueryWrapper<SysUser> userQueryWrapper= new QueryWrapper();
        userQueryWrapper.lambda().eq(SysUser::getUserName,username);
        SysUser sysUser =  sysUserMapper.selectOne(userQueryWrapper);
        handlerSysUser(sysUser,password);
    }

    public User getLogin(LoginReqest reqest)throws Exception{
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(User::getUserCellphone,reqest.getUserPhone());
        User user= userMapper.selectOne(queryWrapper);
        handlerUser(user);
        if (StringUtils.isNotEmpty(reqest.getCode())){
            sysService.inspectSms(reqest.getUserPhone(),reqest.getCode(),reqest.getSmsType());
        }else{
            handlerUserpsw(Md5Utls.verify(reqest.getPassword(),user.getPassword()));
        }
        return user;
    }

    public User register(UserReuqest reuqest)throws Exception{
        List<Long> areaList=new ArrayList<>();
        if (StringUtils.isNoneEmpty(reuqest.getServicesAreaCodes())){
            String[] servicesAreas = reuqest.getServicesAreaCodes().split(",");
            for (String area:servicesAreas){
                Region region= ((Region)redisTemplate.opsForValue().get(regionLayer+area));
                if (region!=null){
                    areaList.add(region.getId());
                }
            }
        }
        userMapper.insert(
                new User(
                        SysConstant.COMPONENT_NAME+LocalDate.now().getYear()+LocalDate.now().getMonthValue()+LocalDate.now().getDayOfMonth()+RandomStringUtils.random(4, false, true),
                        reuqest.getName(),
                        reuqest.getUserCellphone(),
                        Md5Utls.md5(reuqest.getPassword()),
                        ((Region)redisTemplate.opsForValue().get(regionLayer+reuqest.getProvinceCode())).getId(),
                        ((Region)redisTemplate.opsForValue().get(regionLayer+reuqest.getCityCode())).getId(),
                        ((Region)redisTemplate.opsForValue().get(regionLayer+reuqest.getAreaCode())).getId(),
                        reuqest.getMaster(),
                        reuqest.getTeam(),
                        reuqest.getTeamNumber(),
                        StringUtil.join(areaList.toArray(),","),
                        LocalDateTime.now().toString()
                )
        );
        return new User();
    }





}
