package com.fs.hospital.service.impl;

import java.util.Date;
import java.util.List;

import com.fs.common.core.domain.R;
import com.fs.common.utils.DateUtils;
import com.fs.common.utils.SecurityUtils;
import com.fs.hospital.domain.*;
import com.fs.hospital.mapper.*;
import com.fs.hospital.param.HospitalParam;
import com.fs.hospital.vo.HospitalHVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.hospital.service.IHospitalService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 诊所Service业务层处理
 *
 * @author fs
 * @date 2024-02-26
 */
@Service
public class HospitalServiceImpl implements IHospitalService
{
    @Autowired
    private HospitalMapper hospitalMapper;

    @Autowired
    private HospitalDeptMapper deptMapper;

    @Autowired
    private HospitalRoleMapper roleMapper;
    @Autowired
    private HospitalPostMapper postMapper;
    @Autowired
    private HospitalUserMapper userMapper;
    @Autowired
    private HospitalUserRoleMapper userRoleMapper;
    @Autowired
    private HospitalUserPostMapper userPostMapper;

    /**
     * 查询诊所
     *
     * @param hospitalId 诊所主键
     * @return 诊所
     */
    @Override
    public Hospital selectHospitalByHospitalId(Long hospitalId)
    {
        return hospitalMapper.selectHospitalByHospitalId(hospitalId);
    }

    /**
     * 查询诊所列表
     *
     * @param hospital 诊所
     * @return 诊所
     */
    @Override
    public List<Hospital> selectHospitalList(Hospital hospital)
    {
        return hospitalMapper.selectHospitalList(hospital);
    }

    /**
     * 新增诊所
     *
     * @param hospital 诊所
     * @return 结果
     */
    @Override
    @Transactional
    public R insertHospital(Hospital hospital)
    {
        //帐号是否存在
        if(userMapper.checkUserName(hospital.getUserName())>0){
            return R.error("帐号已存在");
        }
        hospital.setCreateTime(DateUtils.getNowDate());
        if(hospitalMapper.insertHospital(hospital)>0){
            //写入帐号信息
            //创建部门
            HospitalDept dept=new HospitalDept();
            dept.setHospitalId(hospital.getHospitalId());
            dept.setParentId(0L);
            dept.setAncestors("0");
            dept.setStatus("0");
            dept.setCreateTime(new Date());
            dept.setDeptName(hospital.getHospitalName());
            deptMapper.insertHospitalDept(dept);
            //创建岗位
            HospitalPost post=new HospitalPost();
            post.setHospitalId(hospital.getHospitalId());
            post.setPostCode("manager");
            post.setPostName("管理员");
            post.setPostSort(0L);
            post.setStatus("0");
            postMapper.insertHospitalPost(post);
            //创建角色
            HospitalRole role=new HospitalRole();
            role.setHospitalId(hospital.getHospitalId());
            role.setRoleName("管理员");
            role.setRoleKey("admin");
            role.setRoleSort(0L);
            role.setStatus(0);
            roleMapper.insertHospitalRole(role);
            //添加用户
            HospitalUser user = new HospitalUser();
            user.setHospitalId(hospital.getHospitalId());
            user.setNickName("管理员");
            user.setPassword(SecurityUtils.encryptPassword(hospital.getPassword()));
            user.setUserName(hospital.getUserName());
            user.setDeptId(dept.getDeptId());
            user.setUserType("00");
            user.setMobile(hospital.getMobile());
            user.setSex("0");
            user.setStatus(0);
            user.setIsDel(0);
            userMapper.insertHospitalUser(user);
            //添加用户角色表
            HospitalUserRole userRole=new HospitalUserRole();
            userRole.setRoleId(role.getRoleId());
            userRole.setUserId(user.getUserId());
            userRoleMapper.insertHospitalUserRole(userRole);
            //添加用户岗位表
            HospitalUserPost userPost=new HospitalUserPost();
            userPost.setPostId(post.getPostId());
            userPost.setUserId(user.getUserId());
            userPostMapper.insertHospitalUserPost(userPost);
            hospital.setUserId(user.getUserId());
            hospitalMapper.updateHospital(hospital);
            return R.ok();
        }
        else
        {
            return R.error("操作失败");
        }
    }

    /**
     * 修改诊所
     *
     * @param hospital 诊所
     * @return 结果
     */
    @Override
    public int updateHospital(Hospital hospital)
    {
        hospital.setUpdateTime(DateUtils.getNowDate());
        return hospitalMapper.updateHospital(hospital);
    }

    /**
     * 批量删除诊所
     *
     * @param hospitalIds 需要删除的诊所主键
     * @return 结果
     */
    @Override
    public int deleteHospitalByHospitalIds(Long[] hospitalIds)
    {
        return hospitalMapper.deleteHospitalByHospitalIds(hospitalIds);
    }

    /**
     * 删除诊所信息
     *
     * @param hospitalId 诊所主键
     * @return 结果
     */
    @Override
    public int deleteHospitalByHospitalId(Long hospitalId)
    {
        return hospitalMapper.deleteHospitalByHospitalId(hospitalId);
    }

    @Override
    public List<HospitalHVO> selectHospitalListVO(HospitalParam param) {
        return hospitalMapper.selectHospitalVOList(param);
    }
}
