package com.ltgk.crew.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ltgk.common.constant.Constants;
import com.ltgk.common.utils.SecurityUtils;
import com.ltgk.crew.domain.entity.DsBoatCrew;
import com.ltgk.crew.domain.entity.DsCrewCredential;
import com.ltgk.crew.mapper.DsBoatCrewMapper;
import com.ltgk.crew.mapper.DsCrewCredentialMapper;
import com.ltgk.crew.service.IDsCrewCredentialService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;

/**
 * <p>
 * 船民证书表 服务实现类
 * </p>
 *
 * @author songzh
 * @since 2022-06-01
 */
@Service
public class DsCrewCredentialServiceImpl extends ServiceImpl<DsCrewCredentialMapper, DsCrewCredential> implements IDsCrewCredentialService {

    @Resource
    private DsBoatCrewMapper dsBoatCrewMapper;

    /**
     * 新增船员证书信息
     *
     * @param dsCrewCredential:
     * @return boolean
     * @author Qi ChengBin
     * @date 2024/12/25 15:52
     */
    @Override
    public boolean saveDsCrewCredential(DsCrewCredential dsCrewCredential) {
        // 校验船员信息在船员档案中是否存在
        LambdaQueryWrapper<DsBoatCrew> crewLambdaQueryWrapper = new LambdaQueryWrapper<>();
        crewLambdaQueryWrapper.eq(DsBoatCrew::getCrewName, dsCrewCredential.getCrewName())
                .eq(DsBoatCrew::getId, dsCrewCredential.getCrewId())
                .eq(DsBoatCrew::getDelFlag, Constants.FLAG_FALSE);
        DsBoatCrew dsBoatCrew = dsBoatCrewMapper.selectOne(crewLambdaQueryWrapper);
        if (Objects.isNull(dsBoatCrew)) {
            throw new RuntimeException("船员档案中不存在'" + dsCrewCredential.getCrewName() + "'");
        }
        String userName = SecurityUtils.getLoginUser().getUser().getNickName();
        // 保存船员证书信息
        dsCrewCredential.setIdCardNo(dsBoatCrew.getIdCardNo())
                .setPhone(dsBoatCrew.getPhone())
                .setAreaCode(dsBoatCrew.getAreaCode())
                .setAreaName(dsBoatCrew.getAreaName())
                .setCreateBy(userName)
                .setCreateAt(new Date())
                .setUpdateBy(userName)
                .setUpdateAt(new Date())
                .setDelFlag(Constants.FLAG_FALSE);

        return this.save(dsCrewCredential);
    }
}
