package com.njzscloud.common.sn;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njzscloud.common.mp.support.PageParam;
import com.njzscloud.common.mp.support.PageResult;
import com.njzscloud.common.sn.support.IncSectionConfig;
import com.njzscloud.common.sn.support.SectionConfig;
import com.njzscloud.common.sn.support.Sn;
import com.njzscloud.common.sn.support.SnUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 编码配置表
 */
@Slf4j
@RequiredArgsConstructor
public class SnConfigService extends ServiceImpl<SnConfigMapper, SnConfigEntity> implements IService<SnConfigEntity> {
    private final IncSnService incSnService;

    @Transactional(rollbackFor = Exception.class)
    public Sn getSn(String sncode) {
        SnConfigEntity configEntity = this.getOne(Wrappers.<SnConfigEntity>lambdaQuery().eq(SnConfigEntity::getSncode, sncode));
        if (configEntity == null) return new Sn();
        List<SectionConfig> configs = configEntity.getConfig();
        return new Sn(configs);
    }

    /**
     * 新增
     */
    @Transactional(rollbackFor = Exception.class)
    public void add(AddSnConfigParam addSnConfigParam) {
        List<SectionConfig> configs = addSnConfigParam.getConfig()
                .stream()
                .map(SnUtil::resolve)
                .collect(Collectors.toList());


        this.save(new SnConfigEntity()
                .setSncode(addSnConfigParam.getSncode())
                .setMemo(addSnConfigParam.getMemo())
                .setConfig(configs)
        );

        List<IncSnEntity> collect = configs
                .stream()
                .filter(it -> it.getClass() == IncSectionConfig.class)
                .map(it -> {
                    IncSectionConfig config = (IncSectionConfig) it;
                    return new IncSnEntity()
                            .setCode(config.getCode())
                            .setVal((long) config.getInitialVal())
                            .setStep(config.getStep())
                            .setInitialVal((long) config.getInitialVal())
                            .setPadMode(config.getPadMode())
                            .setPadVal(config.getPadVal())
                            .setPadLen(config.getPadLen());
                }).collect(Collectors.toList());
        if (collect.isEmpty()) return;
        incSnService.saveBatch(collect);
    }

    /**
     * 修改
     */
    public void modify(ModifySnConfigParam modifySnConfigParam) {
        List<SectionConfig> configs = modifySnConfigParam.getConfig()
                .stream()
                .map(SnUtil::resolve)
                .collect(Collectors.toList());
        this.updateById(new SnConfigEntity()
                .setId(modifySnConfigParam.getId())
                .setMemo(modifySnConfigParam.getMemo())
                .setConfig(configs)
        );
    }

    /**
     * 删除
     */
    @Transactional(rollbackFor = Exception.class)
    public void del(List<Long> ids) {
        this.removeBatchByIds(ids);
    }

    /**
     * 详情
     */
    public SnConfigEntity detail(Long id) {
        return this.getById(id);
    }

    /**
     * 分页查询
     */
    public PageResult<SnConfigEntity> paging(PageParam pageParam, SnConfigEntity snConfigEntity) {
        return PageResult.of(this.page(pageParam.toPage(), Wrappers.<SnConfigEntity>query(snConfigEntity)));
    }

}
