package com.fjhx.service.working.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.base.BaseEntity;
import com.fjhx.entity.working.WorkingProcedure;
import com.fjhx.params.working.WorkingProcedureVo;
import com.fjhx.utils.FileClientUtil;
import com.fjhx.utils.WrapperUtil;
import com.fjhx.mapper.working.WorkingProcedureMapper;
import com.fjhx.service.working.WorkingProcedureService;
import org.springblade.core.secure.utils.AuthUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 工序 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2022-07-14
 */
@Service
public class WorkingProcedureServiceImpl extends ServiceImpl<WorkingProcedureMapper, WorkingProcedure> implements WorkingProcedureService {

    @Override
    public Page<WorkingProcedure> getPage(Map<String, String> condition) {

        QueryWrapper<WorkingProcedure> wrapper = Wrappers.query();

        WrapperUtil.init(condition, wrapper)
                .keyword("name", "name")
                .eq("whether_packing", "whetherPacking")
                .createTimeDesc();

        return page(condition, wrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(WorkingProcedureVo workingProcedureVo) {
        save(workingProcedureVo);
        FileClientUtil.bindingFile(workingProcedureVo.getId(), workingProcedureVo.getFileInfo());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(WorkingProcedureVo workingProcedureVo) {
        updateById(workingProcedureVo);
        FileClientUtil.againBindingFile(workingProcedureVo.getId(), workingProcedureVo.getFileInfo());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(WorkingProcedureVo workingProcedureVo) {
        Long id = workingProcedureVo.getId();
        removeById(id);
        FileClientUtil.relieveBindingFile(id);
    }

    @Override
    public List<WorkingProcedure> getList() {
        return lambdaQuery()
                .select(BaseEntity::getId, WorkingProcedure::getName, WorkingProcedure::getWhetherPacking)
                .eq(BaseEntity::getTenantId, AuthUtil.getTenantId())
                .orderByDesc(BaseEntity::getId)
                .list();
    }

}
