package cn.stylefeng.team.form.modular.component.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.team.form.modular.component.entity.FormComponent;
import cn.stylefeng.team.form.modular.component.enums.FormComponentExceptionEnum;
import cn.stylefeng.team.form.modular.component.mapper.FormComponentMapper;
import cn.stylefeng.team.form.modular.component.pojo.request.FormComponentRequest;
import cn.stylefeng.team.form.modular.component.service.FormComponentService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class FormComponentServiceImpl extends ServiceImpl<FormComponentMapper, FormComponent> implements FormComponentService {

    @Override
    public void add(FormComponentRequest formComponentRequest) {
        FormComponent formComponent = new FormComponent();
        BeanUtil.copyProperties(formComponentRequest, formComponent);
        this.save(formComponent);
    }

    @Override
    public void del(FormComponentRequest formComponentRequest) {
        FormComponent formComponent = this.queryFormComponent(formComponentRequest);
        this.removeById(formComponent.getComponentId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchDelete(FormComponentRequest formComponentRequest) {
        this.removeByIds(formComponentRequest.getBatchDeleteIdList());
    }

    @Override
    public void edit(FormComponentRequest formComponentRequest) {
        FormComponent formComponent = this.queryFormComponent(formComponentRequest);
        BeanUtil.copyProperties(formComponentRequest, formComponent);
        this.updateById(formComponent);
    }

    @Override
    public FormComponent detail(FormComponentRequest formComponentRequest) {
        return this.queryFormComponent(formComponentRequest);
    }

    @Override
    public PageResult<FormComponent> findPage(FormComponentRequest formComponentRequest) {
        LambdaQueryWrapper<FormComponent> wrapper = createWrapper(formComponentRequest);
        Page<FormComponent> pageList = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(pageList);
    }

    @Override
    public List<FormComponent> findList(FormComponentRequest formComponentRequest) {
        LambdaQueryWrapper<FormComponent> wrapper = this.createWrapper(formComponentRequest);
        return this.list(wrapper);
    }

    private FormComponent queryFormComponent(FormComponentRequest formComponentRequest) {
        FormComponent formComponent = this.getById(formComponentRequest.getComponentId());
        if (ObjectUtil.isEmpty(formComponent)) {
            throw new ServiceException(FormComponentExceptionEnum.FORM_COMPONENT_NOT_EXISTED);
        }
        return formComponent;
    }

    private LambdaQueryWrapper<FormComponent> createWrapper(FormComponentRequest formComponentRequest) {
        LambdaQueryWrapper<FormComponent> queryWrapper = new LambdaQueryWrapper<>();

        if (ObjectUtil.isNotEmpty(formComponentRequest.getComponentCategoryId())) {
            queryWrapper.eq(FormComponent::getComponentCategoryId, formComponentRequest.getComponentCategoryId());
        }

        if (ObjectUtil.isNotEmpty(formComponentRequest.getCodeGeneratorFlag())) {
            queryWrapper.eq(FormComponent::getCodeGeneratorFlag, formComponentRequest.getCodeGeneratorFlag());
        }

        String searchText = formComponentRequest.getSearchText();
        if (ObjectUtil.isNotEmpty(searchText)) {
            queryWrapper.like(FormComponent::getComponentName, searchText);
            queryWrapper.or().like(FormComponent::getComponentCode, searchText);
        }

        queryWrapper.orderByAsc(FormComponent::getFldSort);

        return queryWrapper;
    }

}
