package com.mortals.xhx.module.company.service.impl;
import org.springframework.stereotype.Service;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.xhx.module.company.dao.CompanyDao;
import com.mortals.xhx.module.company.model.CompanyEntity;
import com.mortals.xhx.module.company.service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import com.mortals.xhx.module.company.model.CompanyLabelsEntity;
import com.mortals.xhx.module.company.model.CompanyLabelsQuery;
import com.mortals.xhx.module.company.service.CompanyLabelsService;
import org.springframework.util.ObjectUtils;
import java.util.Date;
import java.util.Arrays;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
/**
* CompanyService
* 公司 service实现
*
* @author zxfei
* @date 2023-09-11
*/
@Service("companyService")
@Slf4j
public class CompanyServiceImpl extends AbstractCRUDServiceImpl<CompanyDao, CompanyEntity, Long> implements CompanyService {
    @Autowired
    private CompanyLabelsService companyLabelsService;
    
    @Override
    protected void saveAfter(CompanyEntity entity, Context context) throws AppException {
        if(!ObjectUtils.isEmpty(entity.getCompanyLabelsList())){
            entity.getCompanyLabelsList().stream().peek(item->{
                item.setCompanyId(entity.getId());
                item.setCreateUserId(this.getContextUserId(context));
                item.setCreateTime(new Date());
            }).count();
            companyLabelsService.save(entity.getCompanyLabelsList());
        }
        super.saveAfter(entity, context);
    }

    @Override
    protected void updateAfter(CompanyEntity entity, Context context) throws AppException {
        if(!ObjectUtils.isEmpty(entity.getCompanyLabelsList())){
            Long[] companyLabelsIds = companyLabelsService.find(new CompanyLabelsQuery().companyId(entity.getId())).stream().map(CompanyLabelsEntity::getId).toArray(Long[]::new);
            companyLabelsService.remove(companyLabelsIds,context);
            entity.getCompanyLabelsList().stream().peek(item ->{
                item.setCompanyId(entity.getId());
                item.setCreateUserId(this.getContextUserId(context));
                item.setCreateTime(new Date());
                item.setUpdateUserId(this.getContextUserId(context));
                item.setUpdateTime(new Date());
            }).count();
            companyLabelsService.save(entity.getCompanyLabelsList());
        }
        super.updateAfter(entity, context);
    }

    @Override
    protected void removeAfter(Long[] ids, Context context, int result) throws AppException {
        List<CompanyLabelsEntity> companyLabelslist = companyLabelsService.find(new CompanyLabelsQuery().companyIdList(Arrays.asList(ids)));
        companyLabelsService.removeList(companyLabelslist,context);
        super.removeAfter(ids, context, result);
    }
}