package com.bonus.boot.manager.basic.service.impl;

import com.bonus.boot.manager.basic.dao.PersonnelLibraryDao;
import com.bonus.boot.manager.basic.entity.PersonnelLibraryBean;
import com.bonus.boot.manager.basic.entity.ZNode;
import com.bonus.boot.manager.basic.service.PersonnelLibraryService;
import com.bonus.boot.manager.manager.dao.UtilDao;
import com.bonus.boot.manager.manager.utils.StringHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 包名称：com.bonus.boot.manager.basic.service.impl
 * 类名称：PersonnelLibraryServiceImpl
 * 类描述：
 * 创建人：@author tqzhang
 * 创建时间：2023年05月31日 17:11
 */

@Service("personnelLibraryService")
public class PersonnelLibraryServiceImpl implements PersonnelLibraryService {
    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    @Resource
    private PersonnelLibraryDao dao;
    @Resource
    private UtilDao utilDao;
    @Override
    public List<PersonnelLibraryBean> getList(PersonnelLibraryBean o) {
        return dao.getList(o);
    }

    @Override
    public int addInfo(PersonnelLibraryBean bean) {

        int existNameNum  = utilDao.determineIfExists("pm_base_physical","phy_name",bean.getPersonName(),"");
        if(existNameNum > 0){
            // 姓名已存在
            return 6;
        }
        int num = utilDao.determineIfExists("pm_base_physical","idcard",bean.getIdNumber(),"");
        if(num > 0){
            num = 3;
        }else {
            // 检查手机号是否已存在于数据库中
            int existPhoneNum = utilDao.findteltpone(bean.getPhone());
            if (existPhoneNum > 0) {
                // 手机号已存在
                return 5;
            }
            bean.setAge(StringHelper.countAge(bean.getIdNumber()));
            if (bean.getAge() < 18 || bean.getAge() > 99){
                return 4;
            }
            if ("男".equals(StringHelper.judgeGender(bean.getIdNumber()))){
                bean.setSex("0");
            }else{
                bean.setSex("1");
            }
            bean.setPassword(passwordEncoder.encode("YNsbd@123456"));
            dao.addInfo(bean);
            //是否不为0
//            if(!Objects.equals(bean.getSpecialPost(), "0"))
//            {
//                dao.AddInf(bean);
//            }

            num=dao.addresult(bean.getHospId());
        }
        return num;
    }

    @Override
    public int updateInfo(PersonnelLibraryBean bean) {
        int num = utilDao.determineIfExists("pm_base_physical","idcard",bean.getIdNumber(),bean.getId()+"");
        if(num > 0){
            num = 3;
        }else {
        /*    // 检查手机号是否已存在于数据库中
            int existPhoneNum = utilDao.findteltpone(bean.getPhone());
            if (existPhoneNum > 0) {
                // 手机号已存在
                return 5;
            }*/
            bean.setAge(StringHelper.countAge(bean.getIdNumber()));
            bean.setSex(StringHelper.judgeGender(bean.getIdNumber()));
            num = dao.updateInfo(bean);
            dao.updatete(bean);
        }
        return num;
    }

    @Override
    public PersonnelLibraryBean getListById(String id) {
        return dao.getListById(id);
    }

    @Override
    public int delById(PersonnelLibraryBean bean) {
         int num = dao.delById(bean);
            if(num > 0){

            }
        return num;
    }

    @Override
    public int resetPassword(PersonnelLibraryBean bean) {
        bean.setPassword(passwordEncoder.encode("YNsbd@123456"));
        return dao.resetPassword(bean);
    }

    @Override
    public List<ZNode> getDepartmentTree() {
        return dao.getDepartmentTree();
    }
}
