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 SysSnConfigService extends ServiceImpl<SysSnConfigMapper, SysSnConfigEntity> implements IService<SysSnConfigEntity> {
    private final SysIncSnService sysIncSnService;

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

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


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

        List<SysIncSnEntity> collect = configs
                .stream()
                .filter(it -> it.getClass() == IncSectionConfig.class)
                .map(it -> {
                    IncSectionConfig config = (IncSectionConfig) it;
                    return new SysIncSnEntity()
                            .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;
        sysIncSnService.saveBatch(collect);
    }

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

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

    /**
     * 详情
     *
     * @param id Id
     * @return SysSnConfigEntity 结果
     */
    public SysSnConfigEntity detail(Long id) {
        return this.getById(id);
    }

    /**
     * 分页查询
     *
     * @param sysSnConfigEntity 筛选条件
     * @param pageParam         分页参数
     * @return PageResult&lt;SysSnConfigEntity&gt; 分页结果
     */
    public PageResult<SysSnConfigEntity> paging(PageParam pageParam, SysSnConfigEntity sysSnConfigEntity) {
        return PageResult.of(this.page(pageParam.toPage(), Wrappers.<SysSnConfigEntity>query(sysSnConfigEntity)));
    }

}
