package org.dromara.work.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.system.domain.SysUser;
import org.dromara.work.domain.TpOrderRecord;
import org.dromara.work.domain.bo.TpOrderRecordBo;
import org.dromara.work.domain.vo.TpOrderRecordVo;
import org.dromara.work.mapper.TpOrderRecordMapper;
import org.dromara.work.service.ITpOrderRecordService;
import org.springframework.stereotype.Service;

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

/**
 * 操作历史Service业务层处理
 *
 * @author Maosw
 * @date 2024-08-12
 */
@RequiredArgsConstructor
@Service
public class TpOrderRecordServiceImpl extends MPJBaseServiceImpl<TpOrderRecordMapper, TpOrderRecord> implements ITpOrderRecordService {

    private final TpOrderRecordMapper baseMapper;

    /**
     * 查询操作历史
     *
     * @param id 主键
     * @return 操作历史
     */
    @Override
    public TpOrderRecordVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询操作历史列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 操作历史分页列表
     */
    @Override
    public TableDataInfo<TpOrderRecordVo> queryPageList(TpOrderRecordBo bo, PageQuery pageQuery) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        //和Mybatis plus一致，MPJLambdaWrapper的泛型必须是主表的泛型，并且要用主表的Mapper来调用
        MPJLambdaWrapper<TpOrderRecord> wrapper = buildQueryMPJWrapper(bo)
            .selectAll(TpOrderRecord.class)
            .selectAs(SysUser::getRealName,TpOrderRecordVo::getAddUserName)
            .leftJoin(SysUser.class,SysUser::getUserId,TpOrderRecord::getAddUser)
            .eq(loginUser.getIdentity() == 3,TpOrderRecord::getState,1L)
            .orderByDesc(TpOrderRecord::getId);

        //分页查询 （需要启用 mybatis plus 分页插件）
        IPage<TpOrderRecordVo> listPage = baseMapper.selectJoinPage(new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize()), TpOrderRecordVo.class, wrapper);
        return TableDataInfo.build(listPage);
    }


    /**
     * 查询符合条件的操作历史列表
     *
     * @param bo 查询条件
     * @return 操作历史列表
     */
    @Override
    public List<TpOrderRecordVo> queryList(TpOrderRecordBo bo) {
        //和Mybatis plus一致，MPJLambdaWrapper的泛型必须是主表的泛型，并且要用主表的Mapper来调用
        MPJLambdaWrapper<TpOrderRecord> wrapper = buildQueryMPJWrapper(bo)
            .selectAll(TpOrderRecord.class)
            .select("s.real_name as addUserName")
            .leftJoin("sys_user s on s.user_id = t.add_user")
            .orderByDesc(TpOrderRecord::getId);
        return baseMapper.selectVoList(wrapper);
    }

    private MPJLambdaWrapper<TpOrderRecord> buildQueryMPJWrapper(TpOrderRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        MPJLambdaWrapper<TpOrderRecord> lqw = new MPJLambdaWrapper<>();
        lqw.eq(bo.getOid() != null, TpOrderRecord::getOid, bo.getOid());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), TpOrderRecord::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), TpOrderRecord::getContent, bo.getContent());
        lqw.eq(bo.getAddUser() != null, TpOrderRecord::getAddUser, bo.getAddUser());
        lqw.eq(bo.getAddTime() != null, TpOrderRecord::getAddTime, bo.getAddTime());
        lqw.eq(bo.getState() != null, TpOrderRecord::getState, bo.getState());
        return lqw;
    }

    /**
     * 新增操作历史
     *
     * @param bo 操作历史
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(TpOrderRecordBo bo) {
        TpOrderRecord add = MapstructUtils.convert(bo, TpOrderRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改操作历史
     *
     * @param bo 操作历史
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(TpOrderRecordBo bo) {
        TpOrderRecord update = MapstructUtils.convert(bo, TpOrderRecord.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TpOrderRecord entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除操作历史信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
