package com.hzs.system.sys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.constant.SystemFieldConstants;
import com.hzs.common.core.enums.EYesNo;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.domain.system.sys.SysCorp;
import com.hzs.common.domain.system.sys.ext.SysCorpExt;
import com.hzs.system.sys.mapper.SysCorpMapper;
import com.hzs.system.sys.param.SysCorpParam;
import com.hzs.system.sys.service.ISysCorpService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 组织(公司)设置 服务实现类
 * </p>
 *
 * @author hzs
 * @since 2023-11-20
 */
@Service
public class SysCorpServiceImpl extends ServiceImpl<SysCorpMapper, SysCorp> implements ISysCorpService {

    @Override
    public void saveSysCorp(SysCorpParam sysCorpParam, Long pkMember) {
        if(sysCorpParam.getPkId() == null){
            SysCorp sysCorp = BeanUtil.copyProperties(sysCorpParam, SysCorp.class);
            sysCorp.setPkCreator(pkMember);
            baseMapper.insert(sysCorp);
        }else{
            SysCorp sysCorp = baseMapper.selectById(sysCorpParam.getPkId());
            sysCorp.setPkFatherOrg(sysCorpParam.getPkFatherOrg());
            sysCorp.setCorpName(sysCorpParam.getCorpName());
            sysCorp.setShortName(sysCorpParam.getShortName());
            sysCorp.setPhone(sysCorpParam.getPhone());
            sysCorp.setPrincipal(sysCorpParam.getPrincipal());
            sysCorp.setPkModified(pkMember);
            sysCorp.setModifiedTime(DateUtils.currentDateTime());
            baseMapper.updateById(sysCorp);
        }
    }

    @Override
    public List<SysCorpExt> querySysCorpByCondition(SysCorpParam sysCorpParam) {
        return baseMapper.querySysCorpByCondition(sysCorpParam);
    }

    @Override
    public void deleteSysCorpByPk(Integer pkId, Long pkMember) {
        // 验证公司是否使用
        UpdateWrapper<SysCorp> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set(SystemFieldConstants.DEL_FLAG, EYesNo.NO.getIntValue());
        updateWrapper.set(SystemFieldConstants.PK_MODIFIED, pkMember);
        updateWrapper.set(SystemFieldConstants.MODIFIED_TIME, DateUtils.currentDateTime());
        updateWrapper.eq(SystemFieldConstants.PK_ID, pkId);
        baseMapper.delete(updateWrapper);
    }

    @Override
    public SysCorp getSysCorp(Integer pkId, Integer pkCountry) {
        QueryWrapper<SysCorp> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(SystemFieldConstants.PK_COUNTRY, pkCountry);
        queryWrapper.eq(SystemFieldConstants.ENABLE_STATE, EYesNo.YES.getIntValue());
        queryWrapper.eq(SystemFieldConstants.PK_ID, pkId);
        return baseMapper.selectOne(queryWrapper);
    }
}
