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.HhrUser;
import com.winsun.bean.School;
import com.winsun.bean.SysUser;
import com.winsun.item.core.shiro.ShiroKit;
import com.winsun.mapper.HhrUserMapper;
import com.winsun.mapper.SchoolMapper;
import com.winsun.mapper.SysUserMapper;
import com.winsun.utils.IDCardUtil;
import com.winsun.utils.PicturesUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
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.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Map;

/**
 * @Author: hsl
 * @Date: 2020/4/21 11:19
 */
@RequestMapping("mobileTerminalUser")
@RestController
@Slf4j
public class MobileTerminalUserController {

    private static String PATHTYPE="idCard";

    private static HhrUserMapper hhrUserMapper;

    private static SysUserMapper sysUserMapper;

    private static SchoolMapper schoolMapper;

    @Autowired
    public MobileTerminalUserController(HhrUserMapper hhrUserMapper,SysUserMapper sysUserMapper,SchoolMapper schoolMapper){
        MobileTerminalUserController.hhrUserMapper=hhrUserMapper;
        MobileTerminalUserController.sysUserMapper=sysUserMapper;
        MobileTerminalUserController.schoolMapper=schoolMapper;
    }

    @RequestMapping("selectAddress")
    public ResponseData<List<String>> selectAddress(){
        return ResponseData.success(schoolMapper.selectAdress());
    }

    @RequestMapping("selectSchool")
    public ResponseData<List<Map<String, Object>>> selectSchool(String subName){
        Wrapper<School> wrapper = new EntityWrapper<>();
        wrapper.eq("sub_name",subName);
        wrapper.setSqlSelect("id","school_name");
        List<Map<String, Object>> list = schoolMapper.selectMaps(wrapper);
        return ResponseData.success(list);
    }

    @RequestMapping("registeredAccount")
    public ResponseData<String> registeredAccount(@RequestParam("gender") String gender,@RequestParam("username") String username, @RequestParam("phone") String  phone,
                                                  @RequestParam("idCard") String idCard, @RequestParam("stuCard") String stuCard, @RequestParam("idcardZ") String idcardZ,
                                                  @RequestParam("password") String password, @RequestParam("cpassword") String cpassword, @RequestParam("school") String school,
                                                  @RequestParam("classZ") String classZ, @RequestParam("substName") String substName, @RequestParam("account") String account) throws NoSuchAlgorithmException {
       if(StringUtils.isBlank(gender)){
          return ResponseData.error("性别不能为空！");
       }
        if(StringUtils.isBlank(username)){
            return ResponseData.error("姓名不能为空！");
        }
        if(StringUtils.isBlank(phone)){
            return ResponseData.error("电话不能为空！");
        }
        if(phone.length() != 11){
            return ResponseData.error("手机号码有误！");
        }
        if(StringUtils.isBlank(idCard)){
            return ResponseData.error("身份证号码不能为空！");
        }
        if(!IDCardUtil.IDCardValidate(idCard)){
            return ResponseData.error("身份证号码有误！");
        }
        if(StringUtils.isBlank(stuCard)){
            return ResponseData.error("请上传学生证！");
        }
        if(StringUtils.isBlank(idcardZ)){
            return ResponseData.error("请上传身份证！");
        }
        if(StringUtils.isBlank(password)){
            return ResponseData.error("密码不能为空！");
        }
        if(StringUtils.isBlank(cpassword)){
            return ResponseData.error("原密码不能为空！");
        }
        if(!password.equals(cpassword)){
            return ResponseData.error("密码和原密码不同！");
        }
        if(StringUtils.isBlank(school)){
            return ResponseData.error("学校不能为空！");
        }
        if(StringUtils.isBlank(substName)){
            return ResponseData.error("县分不能为空！");
        }
        if(StringUtils.isBlank(classZ)){
            return ResponseData.error("年级不能为空！");
        }
        if(StringUtils.isBlank(account)){
            return ResponseData.error("账号不能为空！");
        }
        String pattern = "^(?![A-Za-z]+$)(?![A-Z\\d]+$)(?![A-Z\\W]+$)(?![a-z\\d]+$)(?![a-z\\W]+$)(?![\\d\\W]+$)\\S{8,}$";
        if (!password.matches(pattern)) {
            return ResponseData.error( "密码强制等级低，必须要数字、小写英文字母、大写英文字母或特殊字符混合组成，长度至少8位！");
        }
        if (password.contains(account)) {
            return  ResponseData.error("密码与账号信息吻合，禁止使用！");
        }
        Wrapper<SysUser> wrapper = new EntityWrapper<>();
        wrapper.eq("account",account);
        List<SysUser> users = sysUserMapper.selectList(wrapper);
        if(users.size() != 0){
            return ResponseData.error("账号已被使用，请换一个账号！");
        }

        Wrapper<HhrUser> wrapper1 = new EntityWrapper<>();
        wrapper1.eq("id_card",account);
        List<HhrUser> hhrUsers = hhrUserMapper.selectList(wrapper1);
        if(hhrUsers.size() != 0){
            return ResponseData.error("一个身份证只能创建一个账号，请换一个身份证！");
        }

        int insert = 0;
        SysUser sysUser = new SysUser();
        sysUser.setSex(gender);
        sysUser.setAccount(account);
        sysUser.setName(username);
        sysUser.setPhone(phone);
        sysUser.setSalt(ShiroKit.getRandomSalt(5));
        sysUser.setSubstName(substName);
        sysUser.setPassword(ShiroKit.md5(password, sysUser.getSalt()));
        insert =+ sysUserMapper.insert(sysUser);
        insert =+ sysUserMapper.insertUserSchool(sysUser.getId(), school);
        HhrUser hhrUser = new HhrUser();
        hhrUser.setId(sysUser.getId());
        hhrUser.setIdCard(idCard);
        hhrUser.setIdcardImgUrl(PicturesUtil.uploadPictures(idcardZ, sysUser.getId(),PATHTYPE));
        hhrUser.setStudentImgUrl(PicturesUtil.uploadPictures(stuCard, sysUser.getId(),PATHTYPE));
        hhrUser.setGrade(classZ);
        insert =+ hhrUserMapper.insert(hhrUser);
        if (insert == 3){
            return ResponseData.success("账号创建成功!");
        }
        if (insert == 0){
            return ResponseData.error("账号创建失败！");
        }
        return ResponseData.error("账号创建异常，请联系管理员！");
    }

}
