package com.kcim.service.impl;

import com.kcim.common.util.UserContext;
import com.kcim.dao.model.CostChildColumn;
import com.kcim.dao.repository.CostChildColumnRepository;
import com.kcim.service.CostChildColumnService;
import com.kcim.web.request.ChildColumnRequest;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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


@Service("costChildColumnService")
@AllArgsConstructor
@Slf4j
public class CostChildColumnServiceImpl  implements CostChildColumnService {
    CostChildColumnRepository repository;



    /**
     * @param childColumn
     */
    @Override
    public void addChildColumn(ChildColumnRequest childColumn) {
        Long parentCode = childColumn.getParentCode();

        repository.removeByParentCode(parentCode);

        List<CostChildColumn> childColumnList = childColumn.getChildColumnList();

        if(!CollectionUtils.isEmpty(childColumnList)){
            List<CostChildColumn> saveList = new ArrayList<>();
            for (CostChildColumn column:childColumnList){
                column.setParentColumnCode(parentCode);
                column.setCreateUser(String.valueOf(UserContext.getCurrentUser().getId()));
                column.setCreateTime(new Date());
                column.setHospId(UserContext.getCurrentLoginHospId());
                column.setId(null);
                saveList.add(column);
            }
            if(!CollectionUtils.isEmpty(saveList)){
                repository.saveBatch(saveList);
            }
        }
    }

    /**
     * @param columnCode
     * @return
     */
    @Override
    public Object getChildColumnList(Long columnCode) {
        return null;
    }
}