package com.hzya.frame.sysnew.person.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzya.frame.sysnew.person.entity.SysPersonEntity;
import com.hzya.frame.sysnew.person.dao.ISysPersonDao;
import com.hzya.frame.sysnew.person.service.ISysPersonService;
import com.hzya.frame.sysnew.popedomData.entity.SysPopedomDataEntity;
import com.hzya.frame.sysnew.popedomHome.entity.SysPopedomHomeEntity;
import com.hzya.frame.sysnew.popedomInterface.service.impl.InterfaceCache;
import com.hzya.frame.sysnew.popedomMenu.entity.SysPopedomMenuEntity;
import com.hzya.frame.sysnew.popedomOperate.entity.SysPopedomOperateEntity;
import com.hzya.frame.sysnew.role.entity.SysRoleEntity;
import com.hzya.frame.sysnew.user.dao.ISysUserDao;
import com.hzya.frame.sysnew.user.entity.SysUserEntity;
import com.hzya.frame.sysnew.userRoles.entity.SysUserRolesEntity;
import com.hzya.frame.web.entity.BaseResult;
import com.hzya.frame.web.entity.JsonResultEntity;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;

import com.hzya.frame.basedao.service.impl.BaseService;

import java.util.List;

/**
 * 人员表(SysPerson)表服务实现类
 *
 * @author makejava
 * @since 2024-03-26 15:25:12
 */
@Service(value = "sysPersonService")
public class SysPersonServiceImpl extends BaseService<SysPersonEntity, String> implements ISysPersonService {

    private ISysPersonDao sysPersonDao;
    @Resource
    private InterfaceCache interfaceCache;
    @Resource
    private ISysUserDao sysUserDao;
    @Autowired
    public void setSysPersonDao(ISysPersonDao dao) {
        this.sysPersonDao = dao;
        this.dao = dao;
    }

    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 查询人员列表分页
     * @Date 11:52 上午 2024/3/27
     **/
    @Override
    public JsonResultEntity queryEntityPage(JSONObject jsonObject) {
        SysPersonEntity entity = getData("jsonStr", jsonObject, SysPersonEntity.class);
        //判断分页
        if (entity == null || entity.getPageNum() == null || entity.getPageSize() == null) {
            return BaseResult.getFailureMessageEntity("分页查询参数不存在");
        }
        PageHelper.startPage(entity.getPageNum(), entity.getPageSize());
        List<SysPersonEntity> list = sysPersonDao.queryByLike(entity);
        PageInfo pageInfo = new PageInfo(list);
        return BaseResult.getSuccessMessageEntity("查询数据成功", pageInfo);
    }

    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 查询人员列表
     * @Date 11:52 上午 2024/3/27
     **/
    @Override
    public JsonResultEntity queryEntity(JSONObject jsonObject) {
        SysPersonEntity entity = getData("jsonStr", jsonObject, SysPersonEntity.class);
        if(entity == null ){
            entity = new SysPersonEntity();
        }
        List<SysPersonEntity> list = sysPersonDao.queryByLike(entity);
        return BaseResult.getSuccessMessageEntity("查询数据成功", list);
    }

    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 保存人员
     * @Date 2:36 下午 2024/3/27
     **/
    @Override
    public JsonResultEntity saveEntity(JSONObject jsonObject) {
        SysPersonEntity entity = getData("jsonStr", jsonObject, SysPersonEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("参数不允许为空");
        }
        if (entity.getPersonName() == null || "".equals(entity.getPersonName())) {
            return BaseResult.getFailureMessageEntity("请先输入人员名称");
        }
        if (entity.getPersonCode() == null || "".equals(entity.getPersonCode())) {
            return BaseResult.getFailureMessageEntity("请先输入工号");
        }
        if (entity.getMobilePhone() == null || "".equals(entity.getMobilePhone())) {
            return BaseResult.getFailureMessageEntity("请先输入手机号码");
        }
        entity.setCreate();
        //保存
        sysPersonDao.save(entity);
        return BaseResult.getSuccessMessageEntity("保存人员成功", entity);
    }

    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 获取人员
     * @Date 2:36 下午 2024/3/27
     **/
    @Override
    public JsonResultEntity getEntity(JSONObject jsonObject) {
        SysPersonEntity entity = getData("jsonStr", jsonObject, SysPersonEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("参数不允许为空");
        }
        if (entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        entity = sysPersonDao.get(entity.getId());
        if(entity == null ){
            return BaseResult.getFailureMessageEntity("获取人员失败");
        }
        return BaseResult.getSuccessMessageEntity("获取人员成功", entity);
    }

    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 修改人员
     * @Date 2:37 下午 2024/3/27
     **/
    @Override
    public JsonResultEntity updateEntity(JSONObject jsonObject) {
        SysPersonEntity entity = getData("jsonStr", jsonObject, SysPersonEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("参数不允许为空");
        }
        if (entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getPersonName() == null || "".equals(entity.getPersonName())) {
            return BaseResult.getFailureMessageEntity("请先输入人员名称");
        }
        if (entity.getPersonCode() == null || "".equals(entity.getPersonCode())) {
            return BaseResult.getFailureMessageEntity("请先输入工号");
        }
        if (entity.getMobilePhone() == null || "".equals(entity.getMobilePhone())) {
            return BaseResult.getFailureMessageEntity("请先输入手机号码");
        }
        entity.setUpdate();
        //保存
        sysPersonDao.update(entity);
        return BaseResult.getSuccessMessageEntity("修改人员成功", entity);
    }

    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 删除人员
     * @Date 2:37 下午 2024/3/27
     **/
    @Override
    public JsonResultEntity deleteEntity(JSONObject jsonObject) {
        SysPersonEntity entity = getData("jsonStr", jsonObject, SysPersonEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("参数不允许为空");
        }
        if (entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if(!checkUse(entity)){
            return BaseResult.getFailureMessageEntity("当前人员已经使用，不允许删除");
        }
        entity.setUpdate();
        sysPersonDao.logicRemove(entity);
        return BaseResult.getSuccessMessageEntity("删除人员成功");
    }

    /**
     * @Author lvleigang
     * @Description  判断当前人员是否已经被使用
     * @Date 10:04 上午 2024/3/28
     * @param entity
     * @return boolean
     **/
    private boolean checkUse(SysPersonEntity entity) {
        SysUserEntity sysUserEntity = new SysUserEntity();
        sysUserEntity.setPersonId(entity.getId());
        if(sysUserDao.getCount(sysUserEntity) > 0){
            return false;
        }
        return true;
    }

    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 启用停用人员
     * @Date 2:37 下午 2024/3/27
     **/
    @Override
    public JsonResultEntity enableDisableEntity(JSONObject jsonObject) {
        SysPersonEntity entity = getData("jsonStr", jsonObject, SysPersonEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("参数不允许为空");
        }
        if (entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getState() == null || "".equals(entity.getState())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        //0启用 1 停用
        if("0".equals(entity.getState())){
            entity.setUpdate();
            sysPersonDao.update(entity);
            return BaseResult.getSuccessMessageEntity("启用人员成功");
        }else {
            //删除用户角色表
            SysUserEntity sysUserEntity = new SysUserEntity();
            sysUserEntity.setPersonId(entity.getId());
            sysUserEntity.setState(entity.getState());
            sysUserEntity.setUpdate();
            sysUserDao.update(sysUserEntity);
            interfaceCache.reloadData("1");
            entity.setUpdate();
            sysPersonDao.update(entity);
            return BaseResult.getSuccessMessageEntity("停用人员成功");
        }
    }
}
