package com.yida.data.school.news.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yida.data.common.core.entity.school.EduSchoolManuscriptAuthor;
import com.yida.data.common.core.entity.system.Dept;
import com.yida.data.common.core.entity.system.EduYidaApp;
import com.yida.data.common.core.utils.AppUtil;
import com.yida.data.common.core.utils.Asserts;
import com.yida.data.common.service.CommonService;
import com.yida.data.school.news.mapper.EduSchoolManuscriptAuthorMapper;
import com.yida.data.school.news.service.EduSchoolManuscriptAuthorService;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import lombok.RequiredArgsConstructor;

/**
 * Service实现
 *
 * @author zhanghaijun
 * @date 2021-07-19 17:21:58
 */
@Service
@RequiredArgsConstructor
public class EduSchoolManuscriptAuthorServiceImpl extends ServiceImpl
    <EduSchoolManuscriptAuthorMapper, EduSchoolManuscriptAuthor> implements EduSchoolManuscriptAuthorService {

    private final CommonService commonService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveAuthor(EduSchoolManuscriptAuthor author) {
        if (author.getId() != null) {
            EduSchoolManuscriptAuthor exist = getById(author.getId());
            Asserts.isTrue(author.getMobile().equals(exist.getMobile()), "不能修改手机号");
        }
        saveOrUpdate(author);
        Dept school = commonService.getDept(author.getSchoolId());
        if (author.getId() != null && school.getSchoolType().contains(Dept.TYPE_YIDA_APP)) {
            EduYidaApp yidaApp = commonService.getYidaApp(author.getSchoolId());
            author.setYidaAppUserId(AppUtil.userSave(author.getMobile(), author.getName(), yidaApp));
            updateById(author);
        }
    }
}
