package org.nl.system.service.coderule.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.nl.common.domain.query.PageQuery;
import org.nl.common.utils.SecurityUtils;
import org.nl.system.service.coderule.ISysCodeRuleDetailService;
import org.nl.system.service.coderule.dao.SysCodeRuleDetail;
import org.nl.system.service.coderule.dao.mapper.SysCodeRuleDetailMapper;
import org.nl.system.service.coderule.dto.CodeRuleDetailQuery;
import org.nl.system.service.coderule.utils.CodeRuleTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 * 编码规则明细表 服务实现类
 * </p>
 *
 * @author generator
 * @since 2022-12-20
 */
@Service
public class SysCodeRuleDetailServiceImpl extends ServiceImpl<SysCodeRuleDetailMapper, SysCodeRuleDetail> implements ISysCodeRuleDetailService {
    @Autowired
    private SysCodeRuleDetailMapper codeRuleDetailMapper;
    @Override
    public IPage<SysCodeRuleDetail> queryAll(CodeRuleDetailQuery form, PageQuery page) {
        LambdaQueryWrapper<SysCodeRuleDetail> lam = new LambdaQueryWrapper<>();
        lam.eq(SysCodeRuleDetail::getCode_rule_id, form.getId())
                .orderByAsc(SysCodeRuleDetail::getSort_num);
        IPage<SysCodeRuleDetail> pages = new Page<>(page.getPage() + 1, page.getSize());
        codeRuleDetailMapper.selectPage(pages, lam);
        return pages;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(SysCodeRuleDetail codeRuleDetail) {
        String currentUserId = SecurityUtils.getCurrentUserId();
        String currentNickName = SecurityUtils.getCurrentNickName();
        String now = DateUtil.now();
        codeRuleDetail.setId(IdUtil.getSnowflake(1,1).nextIdStr());
        codeRuleDetail.setCurrent_value(codeRuleDetail.getInit_value());
        codeRuleDetail.setCreate_id(currentUserId);
        codeRuleDetail.setCreate_name(currentNickName);
        codeRuleDetail.setCreate_time(now);
        codeRuleDetail.setUpdate_id(currentUserId);
        codeRuleDetail.setUpdate_name(currentNickName);
        codeRuleDetail.setUpdate_time(now);
        if (codeRuleDetail.getType().equals(CodeRuleTypeEnum.DATE.getType())) {
            Date date = DateUtil.date();
            String format = codeRuleDetail.getFormat();
            String nowDate = DateUtil.format(date, format);
            codeRuleDetail.setInit_value(nowDate);
            codeRuleDetail.setCurrent_value(nowDate);
        }
        codeRuleDetailMapper.insert(codeRuleDetail);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(String id) {
        codeRuleDetailMapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysCodeRuleDetail json) {
        codeRuleDetailMapper.updateById(json);
    }
}
