package com.liang.service.impl;

import com.liang.common.constants.CommonConstant;
import com.liang.common.JsonTool;
import com.liang.common.exception.CustomException;
import com.liang.dao.BaseDictDao;
import com.liang.dao.StaActualexpensesDao;
import com.liang.entity.StaReimbursement;
import com.liang.dao.StaReimbursementDao;
import com.liang.service.StaReimbursementService;
import com.liang.vo.dto.StaReimbursementVo;
import com.liang.vo.dto.ViewFileVo;
import io.jsonwebtoken.lang.Collections;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * 报账(StaReimbursement)表服务实现类
 *
 * @author makejava
 * @since 2023-05-16 13:06:38
 */
@Service("staReimbursementService")
public class StaReimbursementServiceImpl implements StaReimbursementService {
    @Resource
    private StaReimbursementDao staReimbursementDao;
    @Resource
    private StaActualexpensesDao staActualexpensesDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public StaReimbursement queryById(Integer id) {
        return this.staReimbursementDao.queryById(id);
    }

    /**
     * 通过FYSQ查询单条数据
     *
     * @param fysq
     * @return
     */
    @Override
    public StaReimbursement queryByFYSQ(Integer fysq) {
        return this.staReimbursementDao.queryByFYSQ(fysq);
    }

    /**
     * 分页查询
     *
     * @param staReimbursement 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<StaReimbursement> queryByPage(StaReimbursement staReimbursement, PageRequest pageRequest) {
        long total = this.staReimbursementDao.count(staReimbursement);
        return new PageImpl<>(this.staReimbursementDao.queryAllByLimit(staReimbursement, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param staReimbursement 实例对象
     * @return 实例对象
     */
    @Override
    public int insert(StaReimbursement staReimbursement) {
        return this.staReimbursementDao.insert(staReimbursement);
    }

    /**
     * 修改数据
     *
     * @param staReimbursement 实例对象
     * @return 实例对象
     */
    @Override
    public int update(StaReimbursement staReimbursement) {
        return this.staReimbursementDao.update(staReimbursement);
    }

    /**
     * 撤销审批状态
     *
     * @param staReimbursement 实例对象
     * @return 实例对象
     */
    @Override
    public int tjrevokeApprove(StaReimbursement staReimbursement) {
        return this.staReimbursementDao.tjrevokeApprove(staReimbursement);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.staReimbursementDao.deleteById(id) > 0;
    }

    @Override
    public List<StaReimbursement> getReimburseList(Map map) {
        return this.staReimbursementDao.getReimburseList(map);
    }

    @Override
    public int batchDelete(List<Integer> idList) {
        staActualexpensesDao.batchDeleteByZb(idList);
        return this.staReimbursementDao.batchDelete(idList);
    }

    @Override
    public int updateSpzt(StaReimbursement staReimbursement) {
        return this.staReimbursementDao.updateSpzt(staReimbursement);
    }

    @Override
    public int updateBZJE(StaReimbursement staReimbursement) {
        return this.staReimbursementDao.updateBZJE(staReimbursement);
    }

    @Resource
    private StaReimbursementService staReimbursementService;
    @Resource
    private BaseDictDao dictDao;

    @Override
    public List<ViewFileVo> viewFile(Integer bzId) {
        StaReimbursement reimbursement = staReimbursementService.queryById(bzId);
        String bxpz = reimbursement.getBxpz();
        AtomicInteger count = new AtomicInteger(0);
        List<StaReimbursementVo> staReimbursementVos = JsonTool.parseArray(bxpz, StaReimbursementVo.class);
        //获取数据库配置 http://localhost:8081//files/
        List<Map<String, String>> dictByCode = dictDao.getDictByCode(CommonConstant.VIEW_FILE_PATH);
        if (Collections.isEmpty(dictByCode)) {
            throw new CustomException("请联系管理员添加文件查看字典");
        }
        String viewFilePath = dictByCode.get(0).get("name");
        if (!Collections.isEmpty(staReimbursementVos)) {
            return staReimbursementVos.stream().map(item -> {
                //从url "http:/1 ocalhost:8082/202305/dcccd096e08a4a1 obaacadae7477d996我的阅读推荐.docx",中截取 dcccd096e08a4a1 obaacadae7477d996我的阅读推荐.docx
                String url = item.getUrl();
                String substring = url.substring(url.lastIndexOf("/", url.lastIndexOf("/") - 1) + 1);
                ;
                String content = "";
                if (substring.endsWith("png") || substring.endsWith("gif") || substring.endsWith("jpeg") || substring.endsWith("jpg")) {
                    //图片 使用img标签拼接 <img frameborder="0" src="http://localhost:8081//files/eaedfdfe22834ed391fc353085741c37_1715051628960.jpg" scrolling="auto" style="auto">
                    content = "<img frameborder=\"0\" src=\"" + viewFilePath + substring + "\" scrolling=\"auto\" style=\"max-width:100%; height: auto\">";
                } else {
                    // 使用 iframe 标签拼接 <iframe frameborder="0" src="http://localhost:8081//files/eaedfdfe22834ed391fc353085741c37_1715051628960.pdf" scrolling="auto" style="width: 99%; height: 950px;"></iframe>
                    content = "<iframe frameborder=\"0\" src=\"" + viewFilePath + substring + "\" scrolling=\"auto\" style=\"width: 99%; height: 950px;\"></iframe>";
                }
                count.getAndIncrement();
                return ViewFileVo.builder()
                        //文件路径  http://localhost:8081//files/eaedfdfe22834ed391fc353085741c37_1715051628960.jpg
                        .content(content)
                        //文件名称
                        .title("图"+count)
                        .build();
            }).collect(Collectors.toList());
        } else {
            throw new CustomException("暂无可查看文件");
        }
    }

    @Override
    public int tjrevokeApprove1(StaReimbursement bzrxm) {
        return this.staReimbursementDao.tjrevokeApprove1(bzrxm);
    }
}
