package com.bonus.sgzb.base.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.bonus.sgzb.base.domain.MaKeeperUser;
import com.bonus.sgzb.base.mapper.DeptMapper;
import com.bonus.sgzb.base.mapper.MaTypeKeeperMapper;
import com.bonus.sgzb.base.service.IMaTypeKeeperService;
import com.bonus.sgzb.base.vo.DeptUser;
import com.bonus.sgzb.base.vo.MaTypeKeeperVO;
import com.bonus.sgzb.system.api.domain.SysDept;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 库管员配置ma_type_keeper(MaTypeKeeper)表服务实现类
 *
 * @author makejava
 * @since 2023-11-28 15:34:03
 */
@Slf4j
@Service("maTypeKeeperService")
public class MaTypeKeeperServiceImpl implements IMaTypeKeeperService {

    @Resource
    private MaTypeKeeperMapper maTypeKeeperMapper;

    @Resource
    private DeptMapper deptMapper;

    /**
     * 根据人员名称查询左侧列表
     * @param userName
     * @return
     */
    @Override
    public List<DeptUser> getMaUserList(String userName) {

        return maTypeKeeperMapper.selectUserList(userName);
    }

    /**
     * 根据userId查询右侧列表
     * @param userId
     * @param typeName
     * @return
     */
    @Override
    public List<MaTypeKeeperVO> getListByMaType(Long userId, String typeName) {

        return maTypeKeeperMapper.selectListByUserId(userId, typeName);
    }

    /**
     * 查询库管员信息
     * @return
     */
    @Override
    public List<MaKeeperUser> getMaTypeKeeper(String sysDept) {
        log.info("MaTypeKeeperServiceImpl getMaTypeKeeper {}", sysDept);
        JSONObject object = JSONObject.parseObject(sysDept);
        String socialCreditCode = object.getString("body");
        //根据企业机构代码（统一社会信用代码）获取企业信息
        SysDept newSysDept = deptMapper.getDeptBySocialCreditCode(socialCreditCode);
        //获取该企业下的库管员
        return maTypeKeeperMapper.getMaTypeKeeper(newSysDept.getDeptId());
    }

    @Override
    public SysDept getEnterprise(String sysDept) {
        log.info("MaTypeKeeperServiceImpl getEnterprise {}", sysDept);
        JSONObject object = JSONObject.parseObject(sysDept);
        String socialCreditCode = object.getString("body");
        //根据企业机构代码（统一社会信用代码）获取企业信息
        SysDept newSysDept = deptMapper.getDeptBySocialCreditCode(socialCreditCode);
        return newSysDept;
    }
}

