package cn.com.ty.lift.enterprise.oa.service;

import cn.com.ty.lift.enterprise.oa.dto.AnnouncementRequest;
import cn.com.ty.lift.enterprise.oa.dto.AnnouncementResponse;
import cn.com.ty.lift.enterprise.oa.entity.Announcement;
import cn.com.ty.lift.enterprise.oa.entity.AnnouncementImg;
import cn.com.ty.lift.enterprise.oa.mapper.AnnouncementMapper;
import cn.com.xwy.boot.web.dto.RestResponse;
import cn.hutool.core.collection.IterUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 公司公告 服务类
 * </p>
 *
 * @author wcz
 * @since 2019-12-12
 */
@Service
@AllArgsConstructor
public class AnnouncementService extends ServiceImpl<AnnouncementMapper,Announcement> {

    private AnnouncementImgService announcementImgService;
    /**
     * 根据条件分页查询公告记录
     * @param request AnnouncementRequest
     * @return IPage<AnnouncementResponse>
     */
    public IPage<AnnouncementResponse> pageByCondition(AnnouncementRequest request) {
        return baseMapper.pageByCondition(request.getPage(),request);
    }

    public int countByMtCompany(Long mtCompanyId){
        LambdaQueryWrapper<Announcement> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(Announcement::getMtCompanyId, mtCompanyId);
        return count(lambdaQueryWrapper);
    }

    private void rollback(){
        //强制手动事务回滚
        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
    }

    @Transactional(rollbackFor = Exception.class)
    public RestResponse removeByIdAndMtCompany(Long id, Long mtCompanyId){
        LambdaQueryWrapper<Announcement> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(Announcement::getId, id);
        lambdaQueryWrapper.eq(Announcement::getMtCompanyId, mtCompanyId);
        boolean an = remove(lambdaQueryWrapper);
        if(!an){
            return RestResponse.fail("删除公告失败，请稍后重试");
        }
        boolean img = announcementImgService.removeByAnnouncement(id);
        if(img){
            return RestResponse.success(true);
        }else{
            rollback();
            return RestResponse.fail("删除公告图片失败，请稍后重试");
        }
    }
    /**
     * 根据id查询，带全部关联信息
     * @param request AnnouncementRequest
     * @return AnnouncementResponse
     */
    public AnnouncementResponse infoById(AnnouncementRequest request){
        return baseMapper.infoById(request);
    }

    /**
     * 新增公告，同时插入图片
     * @param entity Announcement
     * @return boolean
     */
    @Transactional(rollbackFor = Exception.class)
    public RestResponse add(Announcement entity){
        boolean an = save(entity);
        //插入公告失败，直接返回false
        if(!an){
            return RestResponse.fail("新增公告失败，请稍后重试");
        }
        //没有图片，直接返回true
        List<String> imgUrls = entity.getImgUrls();
        if(IterUtil.isEmpty(imgUrls)){
            return RestResponse.success(true);
        }
        //循环处理，批量插入图片
        List<AnnouncementImg> imgList = new ArrayList<>();
        for (int i = 0; i < imgUrls.size(); i++) {
            AnnouncementImg ai = new AnnouncementImg();
            ai.setAnnouncementId(entity.getId());
            ai.setImgUrl(imgUrls.get(i));
            ai.setSort(i);
            imgList.add(ai);
        }
        boolean ais = announcementImgService.saveBatch(imgList, imgList.size());
        if(ais){
            return RestResponse.success(true);
        }else{
            rollback();
            return RestResponse.fail("插入公告图片失败，请稍后重试");
        }
    }
}