package org.dromara.zhishu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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.zhishu.domain.Shop;
import org.dromara.zhishu.domain.XyBind;
import org.dromara.zhishu.mapper.ShopMapper;
import org.dromara.zhishu.mapper.XyBindMapper;
import org.dromara.zhishu.service.IXyBindService;

import org.dromara.zhishu.domain.bo.XyBindBo;
import org.dromara.zhishu.domain.vo.XyBindVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

import static com.baomidou.mybatisplus.extension.toolkit.Db.update;

@RequiredArgsConstructor
@Service
@Slf4j
public class XyBindServiceImpl implements IXyBindService {

    private final XyBindMapper baseMapper;
    private final ShopMapper shopMapper;

    @Override
    public XyBindVo queryById(String token) {
        LambdaQueryWrapper<XyBind> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(XyBind::getToken, token);
        return baseMapper.selectVoOne(wrapper);
    }

    @Override
    public TableDataInfo<XyBindVo> queryPageList(XyBindBo bo, PageQuery pageQuery) {
        // 获取当前用户id
        Long userId = LoginHelper.getUserId();

        // 如果不是管理员，设置创建者条件
        if (userId != 1) {
            bo.setCreateBy(userId);
        }

        // 构建查询条件
        LambdaQueryWrapper<XyBind> lqw = buildQueryWrapper(bo);

        // 执行分页查询
        Page<XyBindVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);

        return TableDataInfo.build(result);
    }

    private LambdaQueryWrapper<XyBind> buildQueryWrapper(XyBindBo bo) {
        LambdaQueryWrapper<XyBind> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(XyBind::getId);

        // 只保留常用搜索条件
        lqw.eq(bo.getCreateBy() != null, XyBind::getCreateBy, bo.getCreateBy());
        lqw.eq(StringUtils.isNotBlank(bo.getAppName()), XyBind::getAppName, bo.getAppName());
        lqw.like(bo.getMallId() != null, XyBind::getMallId, bo.getMallId());
        lqw.eq(StringUtils.isNotBlank(bo.getToken()), XyBind::getToken, bo.getToken());
        return lqw;
    }

    @Override
    public Boolean insertByBo(XyBindBo bo) {
        XyBind xyBind = new XyBind();
        xyBind.setAppName(bo.getAppName());
        xyBind.setMallId(bo.getMallId());
        xyBind.setToken(bo.getToken());
        xyBind.setCreateBy(bo.getCreateBy());
        // 执行插入操作
        return baseMapper.insert(xyBind) > 0;
    }

    @Override
    public Boolean updateByBo(XyBindBo bo) {
        LambdaUpdateWrapper<XyBind> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(XyBind::getToken, bo.getToken())
                .set(XyBind::getAppName, bo.getAppName())
                .set(XyBind::getUpdateBy, bo.getUpdateBy())
                .set(XyBind::getUpdateTime, new Date());

        return baseMapper.update(updateWrapper) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteByToken(String token) {
        // 获取当前登录用户ID
        Long userId = LoginHelper.getUserId();

        // 1. 逻辑删除xy_bind表中的数据
        LambdaUpdateWrapper<XyBind> xyBindUpdateWrapper = new LambdaUpdateWrapper<>();
        xyBindUpdateWrapper.eq(XyBind::getToken, token)
                .set(XyBind::getDelFlag, 1)
                .set(XyBind::getUpdateTime, new Date())
                .set(userId != null, XyBind::getUpdateBy, userId);

        int xyBindResult = baseMapper.update(null, xyBindUpdateWrapper);

         LambdaUpdateWrapper<Shop> shopUpdateWrapper = new LambdaUpdateWrapper<>();
         shopUpdateWrapper.eq(Shop::getToken, token)
                 .set(Shop::getDelFlag, 1)
                 .set(Shop::getUpdateTime, new Date())
                 .set(userId != null, Shop::getUpdateBy, userId);

         int shopResult = shopMapper.update(null, shopUpdateWrapper);


        log.info("删除操作完成 - xy_bind表影响行数: {}, t_shop表影响行数: {}", xyBindResult, shopResult);

        // 只要xy_bind表删除成功就返回true，或者您可以根据业务需求调整
        return xyBindResult > 0;
    }
}