package com.ruoyi.frequency.workstep.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.code.PublicCommon;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.controller.req.WorksStepReq;
import com.ruoyi.frequency.worklike.entity.WorkLike;
import com.ruoyi.frequency.worklike.mapper.WorkLikeMapper;
import com.ruoyi.frequency.works.entity.Works;
import com.ruoyi.frequency.works.mapper.WorksMapper;
import com.ruoyi.frequency.works.service.WorksService;
import com.ruoyi.frequency.workstep.entity.WorkStep;
import com.ruoyi.frequency.workstep.mapper.WorkStepMapper;
import com.ruoyi.frequency.workstep.service.WorkStepService;
import com.ruoyi.response.ResponseData;
import com.ruoyi.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 作品踩Service业务层处理
 *
 * @author liwenlong
 * @date 2023-10-12
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class WorkStepServiceImpl extends ServiceImpl<WorkStepMapper, WorkStep>  implements WorkStepService
{
    @Autowired
    private WorkStepMapper workStepMapper;

    /**
     * 查询作品踩
     *
     * @param id 作品踩主键
     * @return 作品踩
     */
    @Override
    public WorkStep selectWorkStepById(Long id)
    {
        return workStepMapper.selectWorkStepById(id);
    }

    /**
     * 查询作品踩列表
     *
     * @param workStep 作品踩
     * @return 作品踩
     */
    @Override
    public List<WorkStep> selectWorkStepList(WorkStep workStep)
    {
        return workStepMapper.selectWorkStepList(workStep);
    }

    /**
     * 新增作品踩
     *
     * @param workStep 作品踩
     * @return 结果
     */
    @Override
    public int insertWorkStep(WorkStep workStep)
    {
        workStep.setCreateTime(DateUtils.getNowDate());
        return workStepMapper.insertWorkStep(workStep);
    }

    /**
     * 修改作品踩
     *
     * @param workStep 作品踩
     * @return 结果
     */
    @Override
    public int updateWorkStep(WorkStep workStep)
    {
        workStep.setUpdateTime(DateUtils.getNowDate());
        return workStepMapper.updateWorkStep(workStep);
    }

    /**
     * 批量删除作品踩
     *
     * @param ids 需要删除的作品踩主键
     * @return 结果
     */
    @Override
    public int deleteWorkStepByIds(Long[] ids)
    {
        this.lambdaUpdate().set(WorkStep::getDelFlag,PublicCommon.删除).in(WorkStep::getId,ids).update();
        return ids.length;
    }

    /**
     * 删除作品踩信息
     *
     * @param id 作品踩主键
     * @return 结果
     */
    @Override
    public int deleteWorkStepById(Long id)
    {
        this.lambdaUpdate().set(WorkStep::getDelFlag,PublicCommon.删除).eq(WorkStep::getId,id).update();
        return 1;
    }

    @Autowired
    private WorkLikeMapper workLikeMapper;

    @Autowired
    private WorksMapper worksMapper;

    @Override
    public ResponseData worksStep(WorksStepReq req, UserVo userVo) {
        WorkStep workStep = new WorkStep(req);

        //更新帖子点赞记录
        workStepMapper.workStep(workStep,userVo);

        workStep = workStepMapper.selectOne(new QueryWrapper<WorkStep>().lambda()
                .eq(WorkStep::getWorkId, req.getWorkId())
                .eq(WorkStep::getUserType, userVo.getUserType())
                .eq(WorkStep::getUserId, userVo.getUserId())
        );

        workStep = workStepMapper.selectById(workStep.getId());

        //是否踩
        Boolean isStep = workStep.getDelFlag().equals(PublicCommon.启用);


        Works works = new Works().setId(req.getWorkId()).setStepNum(isStep?1:-1);

        if (isStep){
            WorkLike workLike = workLikeMapper.selectOne(new QueryWrapper<WorkLike>().lambda()
                    .eq(WorkLike::getWorkId, req.getWorkId())
                    .eq(WorkLike::getUserType, userVo.getUserType())
                    .eq(WorkLike::getUserId, userVo.getUserId())
            );
            if (workLike != null && workLike.getDelFlag().equals(PublicCommon.启用)){

                workLike.setDelFlag(PublicCommon.禁用);
                //更新作品踩记录
                workLikeMapper.workLike(workLike,userVo);

                works.setLikeNum(-1);
            }
        }

        //更新数量
        works.setUpdateData();
        worksMapper.updateWorks(works);

        return ResponseData.success();
    }
}
