package cn.cslg.report.service.business.asso;

import cn.cslg.report.common.model.dto.PasUserDTO;
import cn.cslg.report.common.model.dto.ShareReportDTO;
import cn.cslg.report.entity.AssoComapareFiel;
import cn.cslg.report.entity.asso.AssoReportPerson;
import cn.cslg.report.mapper.AssoComapareFielMapper;
import cn.cslg.report.mapper.AssoReportPersonMapper;
import cn.cslg.report.service.OutInterfaceService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.v3.oas.models.security.SecurityScheme;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.List;
import java.util.function.Function;

@SuppressWarnings({"all"})
@Service
@Slf4j
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class AssoReportPersonService extends ServiceImpl<AssoReportPersonMapper, AssoReportPerson> {
    private final OutInterfaceService outInterfaceService;
    /**
     * 分享报告
     *
     * @param comapareFiel
     * @throws IOException
     */
    @Transactional(rollbackFor = Exception.class)
    public Integer shareReport(ShareReportDTO shareReportDTO)  {
        int flag = 0;
        List<Integer> ids = shareReportDTO.getPersonIds();
        Integer reportId = shareReportDTO.getReportId();
        if (reportId != null && ids != null && ids.size() != 0) {
            //循环添加
            for (Integer id : ids) {
                AssoReportPerson assoReportPerson = new AssoReportPerson();
                assoReportPerson.setReportId(reportId);
                assoReportPerson.setPersonId(id);
                assoReportPerson.insert();
                flag++;
            }
        }
        return flag;
    }

    /**
     * 移除分享
     *
     * @param assoComapareFiel
     * @throws IOException
     */
    @Transactional(rollbackFor = Exception.class)
    public void removeShare(ShareReportDTO shareReportDTO){
        if (shareReportDTO.getReportId() != null) {
            LambdaQueryWrapper<AssoReportPerson> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(AssoReportPerson::getReportId, shareReportDTO.getReportId());
            if (shareReportDTO.getPersonIds() != null && shareReportDTO.getPersonIds().size() != 0) {
                wrapper.in(AssoReportPerson::getPersonId,shareReportDTO.getPersonIds());
                this.remove(wrapper);
            }

        }
    }

    /**
     * 展示分享人
     *
     * @param assoComapareFiel
     * @throws IOException
     */
    @Transactional(rollbackFor = Exception.class)
    public Object showShare(ShareReportDTO shareReportDTO) throws IOException {
        //根据报告id查询人员id
        LambdaQueryWrapper<AssoReportPerson> wrapper =new LambdaQueryWrapper<>();
        wrapper.select(AssoReportPerson::getPersonId);
        wrapper.eq(AssoReportPerson::getReportId,shareReportDTO.getReportId());
        Function<Object,Integer> f = (o -> Integer.parseInt(o.toString())) ;
        List<Integer> userIds =this.listObjs(wrapper,f);
        IPage<T> iPage =new Page();
        if(userIds==null||userIds.size()==0){
            iPage.setCurrent(shareReportDTO.getCurrent());
               iPage.setSize(shareReportDTO.getSize());
               iPage.setTotal(0);
            return iPage;
        }
        PasUserDTO pasUserDTO =new PasUserDTO();
        pasUserDTO.setUserIds(userIds);
        pasUserDTO.setUsername(shareReportDTO.getUserName());
        pasUserDTO.setName(shareReportDTO.getPersonName());
        pasUserDTO.setCurrent(shareReportDTO.getCurrent());
        pasUserDTO.setSize(shareReportDTO.getSize());
        String res = outInterfaceService.getPASAssignedUser(pasUserDTO);
        Object object =JSON.parseObject(res);
        return object;
    }


}
