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.exception.ServiceException;
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.work.domain.TpOrderModel;
import org.dromara.work.domain.TpOrderRecord;
import org.dromara.work.domain.bo.TpOrderModelBo;
import org.dromara.work.domain.vo.TpOrderModelVo;
import org.dromara.work.mapper.TpOrderModelMapper;
import org.dromara.work.mapper.TpOrderRecordMapper;
import org.dromara.work.service.ITpOrderModelService;
import org.springframework.stereotype.Service;

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

/**
 * 订单模型Service业务层处理
 *
 * @author Maosw
 * @date 2024-08-12
 */
@RequiredArgsConstructor
@Service
public class TpOrderModelServiceImpl extends MPJBaseServiceImpl<TpOrderModelMapper,TpOrderModel> implements ITpOrderModelService {

    private final TpOrderModelMapper baseMapper;

    private final TpOrderRecordMapper recordMapper;

    /**
     * 查询订单模型
     *
     * @param id 主键
     * @return 订单模型
     */
    @Override
    public TpOrderModelVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询订单模型列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 订单模型分页列表
     */
    @Override
    public TableDataInfo<TpOrderModelVo> queryPageList(TpOrderModelBo bo, PageQuery pageQuery) {
        //和Mybatis plus一致，MPJLambdaWrapper的泛型必须是主表的泛型，并且要用主表的Mapper来调用
        MPJLambdaWrapper<TpOrderModel> wrapper = buildQueryMPJWrapper(bo)
            .selectAll(TpOrderModel.class)
            .select("s.real_name as addUserName")
            .leftJoin("sys_user s on s.user_id = t.add_user")
            .orderByDesc(TpOrderModel::getId);

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

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

        return baseMapper.selectVoList(wrapper);
    }

    private MPJLambdaWrapper<TpOrderModel> buildQueryMPJWrapper(TpOrderModelBo bo) {
        Map<String, Object> params = bo.getParams();
        MPJLambdaWrapper<TpOrderModel> lqw = new MPJLambdaWrapper<>();
        lqw.eq(bo.getOid() != null, TpOrderModel::getOid, bo.getOid());
        lqw.like(StringUtils.isNotBlank(bo.getName()), TpOrderModel::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getPic()), TpOrderModel::getPic, bo.getPic());
        lqw.eq(bo.getAddTime() != null, TpOrderModel::getAddTime, bo.getAddTime());
        lqw.eq(bo.getAddUser() != null, TpOrderModel::getAddUser, bo.getAddUser());
        return lqw;
    }

    /**
     * 新增订单模型
     *
     * @param bo 订单模型
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(TpOrderModelBo bo) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (!loginUser.getIdentity().equals(2)){
            throw new ServiceException("只有技术才可以上传图片哦");
        }
        bo.setAddTime(new Date());
        bo.setAddUser(loginUser.getUserId());
        TpOrderModel add = MapstructUtils.convert(bo, TpOrderModel.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            TpOrderRecord record = new TpOrderRecord();
            record.setOid(bo.getOid());
            record.setType("上传模型");
            record.setContent("订单上传模型成功");
            record.setAddUser(loginUser.getUserId());
            record.setAddTime(new Date());
            record.setState(1);
            recordMapper.insert(record);
        }
        return flag;
    }

    /**
     * 修改订单模型
     *
     * @param bo 订单模型
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(TpOrderModelBo bo) {
        TpOrderModel update = MapstructUtils.convert(bo, TpOrderModel.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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