package cn.iocoder.yudao.module.system.service.tenant;

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.enterprise.EnterpriseInfoRespVO;
import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.enterprise.EnterpriseInfoSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.tenant.EnterpriseBankInfoDO;
import cn.iocoder.yudao.module.system.dal.dataobject.tenant.EnterpriseInfoDO;
import cn.iocoder.yudao.module.system.dal.mysql.tenant.EnterpriseBankInfoMapper;
import cn.iocoder.yudao.module.system.dal.mysql.tenant.EnterpriseInfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

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


/**
 * 企业信息 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class EnterpriseInfoServiceImpl implements EnterpriseInfoService {

    @Resource
    private EnterpriseInfoMapper enterpriseInfoMapper;
    @Resource
    private EnterpriseBankInfoMapper enterpriseBankInfoMapper;


    @Override
    public Long createEnterpriseInfo(EnterpriseInfoSaveReqVO createReqVO) {
        EnterpriseInfoDO enterpriseInfo = new EnterpriseInfoDO();
        BeanUtils.copyProperties(createReqVO, enterpriseInfo);
        enterpriseInfo.setId(null);
        enterpriseInfo.setCreatedBy(SecurityFrameworkUtils.getLoginUserNickname());
        enterpriseInfo.setCreatedTime(new Date());
        enterpriseInfoMapper.insert(enterpriseInfo);

        List<EnterpriseBankInfoDO> openAccountList = createReqVO.getOpenAccountList();
        if (CollectionUtil.isNotEmpty(openAccountList)) {
            openAccountList.forEach(e -> {
                e.setCreatedTime(new Date());
                e.setCreatedBy(SecurityFrameworkUtils.getLoginUserNickname());
                e.setTenantId(enterpriseInfo.getTenantId());
                enterpriseBankInfoMapper.insert(e);
            });
        }
        return enterpriseInfo.getId();
    }

    @Override
    public void updateEnterpriseInfo(EnterpriseInfoSaveReqVO updateReqVO) {
        EnterpriseInfoDO enterpriseInfo = new EnterpriseInfoDO();
        BeanUtils.copyProperties(updateReqVO, enterpriseInfo);
        enterpriseInfoMapper.updateById(enterpriseInfo);

        List<EnterpriseBankInfoDO> openAccountList = updateReqVO.getOpenAccountList();
        if (CollectionUtil.isNotEmpty(openAccountList)) {
            // 先删除在重新生成
            enterpriseBankInfoMapper.delete(new LambdaQueryWrapperX<EnterpriseBankInfoDO>()
                    .eq(EnterpriseBankInfoDO::getTenantId, updateReqVO.getTenantId()));

            //重新生成开户信息
            openAccountList.forEach(e -> {
                e.setCreatedTime(new Date());
                e.setCreatedBy(SecurityFrameworkUtils.getLoginUserNickname());
                e.setTenantId(enterpriseInfo.getTenantId());
                enterpriseBankInfoMapper.insert(e);
            });
        }
    }

    @Override
    public EnterpriseInfoRespVO getEnterpriseInfo(Long tenantId) {
        EnterpriseInfoDO enterpriseInfo = enterpriseInfoMapper.selectOne(new LambdaQueryWrapperX<EnterpriseInfoDO>()
                .eq(EnterpriseInfoDO::getTenantId, tenantId));

        if (enterpriseInfo == null){
            return null;
        }

        List<EnterpriseBankInfoDO> openAccountList = enterpriseBankInfoMapper.selectList(new LambdaQueryWrapperX<EnterpriseBankInfoDO>()
                .eq(EnterpriseBankInfoDO::getTenantId, tenantId));

        EnterpriseInfoRespVO respVO = new EnterpriseInfoRespVO();
        BeanUtils.copyProperties(enterpriseInfo, respVO);
        respVO.setOpenAccountList(openAccountList);
        return respVO;
    }
}