package com.jeeplus.modules.sg.managementcenter.reserveManagement.service;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.filing.basicInformation.entity.BasicInformationChild;
import com.jeeplus.modules.sg.financial.settlement.entity.Corresponding;
import com.jeeplus.modules.sg.managementcenter.reserveManagement.entity.Standard;
import com.jeeplus.modules.sg.managementcenter.reserveManagement.mapper.StandardMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;

/**
 * 人员流转 Service
 */
@Service
@Transactional(readOnly = true)
public class StandardService extends CrudService<StandardMapper, Standard> {

    @Override
    public Standard get(Standard entity) {
        Standard standard = mapper.get(entity);
        List<Standard> childList = mapper.findChildList(standard);
        standard.setChildList(childList);
        return standard;
    }

    @Override
    public Standard get(String id) {
        Standard standard = mapper.get(id);
        List<Standard> childList = mapper.findChildList(standard);
        standard.setChildList(childList);
        return standard;
    }

    @Override
    public List<Standard> findList(Standard entity) {
        return super.findList(entity);
    }

    @Override
    public Page<Standard> findPage(Page<Standard> page, Standard entity) {
        return super.findPage(page, entity);
    }

    @Transactional(readOnly = false)
    public void saveChild(Standard entity) {
        List<Standard> childList = entity.getChildList();
        for (Standard c :
                childList) {
            c.setProjectCategory(entity.getProjectCategory());
            if (BasicInformationChild.DEL_FLAG_NORMAL.equals(c.getDelFlag())) {
                if (c.getIsNewRecord()) {
                    c.preInsert();
                    mapper.insertChild(c);
                } else {
                    c.preUpdate();
                    mapper.updateChild(c);
                }
            } else {
                mapper.deleteChild(c);
            }
        }
    }

    /**
     * 查询项目建设明细详细列表
     */
    public Page<Standard> findChildPage(Page<Standard> page, Standard entity) {
        dataRuleFilter(entity);
        entity.setPage(page);
        page.setList(mapper.findChildList(entity));
        return page;
    }

    @Override
    @Transactional(readOnly = false)
    public void save(Standard entity) {
        if (entity.getIsNewRecord()) {
            entity.preInsert();
            mapper.insert(entity);
        } else {
            entity.preUpdate();
            mapper.update(entity);
        }
    }

    @Transactional(readOnly = false)
    public void insert(Standard entity) {
        mapper.insert(entity);
    }

    @Transactional(readOnly = false)
    public void update(Standard entity) {
        mapper.update(entity);
    }

    @Override
    @Transactional(readOnly = false)
    public void deleteAll(Collection<Standard> entitys) {
        super.deleteAll(entitys);
    }

    @Transactional(readOnly = false)
    public void updateChild(Standard entity) {
        Integer byCodeCate = mapper.findByCodeCate(entity);
        if (0 == byCodeCate) {
            entity.preInsert();
            mapper.insertChild(entity);
        } else {
            entity.preUpdate();
            mapper.updateChild(entity);
        }
    }

    @Transactional(readOnly = false)
    public void deleteChildList(List<Standard> list) {
        mapper.deleteChildList(list);
    }

    @Transactional(readOnly = false)
    public void insertChildList(List<Standard> list) {
        for (Standard a :
                list) {
            a.preInsert();
        }
        mapper.insertChildList(list);
    }

    public List<Standard> findALl() {
        return mapper.findALl();
    }


}
