FormServiceImpl.java 2.85 KB
package vion.service.impl;

import org.dromara.hutool.core.data.id.IdUtil;
import org.dromara.hutool.core.util.ObjUtil;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import io.github.linpeilie.Converter;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import vion.dto.FormDTO;
import vion.mapper.FormMapper;
import vion.model.Account;
import vion.model.Form;
import vion.model.Store;
import vion.service.IFormService;
import vion.vo.FormVO;

/**
 * @author HlQ
 * @date 2023/12/12
 */
@Service
@RequiredArgsConstructor
public class FormServiceImpl extends MPJBaseServiceImpl<FormMapper, Form> implements IFormService {

    private final Converter converter;

    @Override
    public Page<FormVO> list(FormDTO dto) {
        MPJLambdaWrapper<Form> wrapper = new MPJLambdaWrapper<>(converter.convert(dto, Form.class))
                .selectAll(Form.class)
                .selectAs(Account::getName, FormVO::getAccountName)
                .selectAs(Store::getName, FormVO::getStoreName)
                .leftJoin(Account.class, Account::getId, Form::getAccountId)
                .leftJoin(Store.class, Store::getId, Form::getStoreId);
        return this.selectJoinListPage(Page.of(dto.getPageNum(), dto.getPageSize()), FormVO.class, wrapper);
    }

    @Override
    public Object save(FormDTO dto) {
        Form form = converter.convert(dto, Form.class);
        String nanoId = IdUtil.nanoId();
        form.setUuid(nanoId);
        return this.save(form) ? JSONUtil.ofObj().set("id", form.getId()).set("uuid", nanoId) : "创建失败";
    }

    @Override
    public String updateById(Long id, FormDTO dto) {
        Form form = converter.convert(dto, Form.class);
        form.setId(id);
        return this.updateById(form) ? "更新成功" : "更新失败";
    }

    @Override
    public FormVO getByUniqueId(Long id, String uuid) {
        MPJLambdaWrapper<Form> wrapper = new MPJLambdaWrapper<Form>()
                .selectAll(Form.class)
                .selectAs(Account::getName, FormVO::getAccountName)
                .selectAs(Store::getName, FormVO::getStoreName)
                .leftJoin(Account.class, Account::getId, Form::getAccountId)
                .leftJoin(Store.class, Store::getId, Form::getStoreId)
                .eq(ObjUtil.isNotNull(id), Form::getId, id)
                .eq(StrUtil.isNotBlank(uuid), Form::getUuid, uuid);
        return this.selectJoinOne(FormVO.class, wrapper);
    }

    @Override
    public String updByUuid(String uuid, FormDTO dto) {
        Form form = converter.convert(dto, Form.class);
        return this.lambdaUpdate().eq(Form::getUuid, uuid).update(form) ? "提交成功" : "提交失败";
    }
}