package com.liang.service.impl;

import com.liang.common.JsonTool;
import com.liang.common.constants.CommonConstant;
import com.liang.common.exception.CustomException;
import com.liang.dao.BaseDictDao;
import com.liang.entity.*;
import com.liang.dao.StaPayapprovalDao;
import com.liang.service.PrjContractService;
import com.liang.service.PrjPayplanService;
import com.liang.service.StaPayapprovalService;
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 org.springframework.transaction.annotation.Transactional;

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

/**
 * 付款申请(StaPayapproval)表服务实现类
 *
 * @author makejava
 * @since 2023-06-05 16:03:50
 */
@Service("staPayapprovalService")
public class StaPayapprovalServiceImpl implements StaPayapprovalService {
    @Resource
    private StaPayapprovalDao staPayapprovalDao;
    @Resource
    private PrjPayplanService prjPayplanService;
    @Resource
    private PrjContractService prjContractService;


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


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

    /**
     * 新增数据
     *
     * @param staPayapproval 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional
    public int insert(StaPayapproval staPayapproval) {
        //以下内容已单独处理
//        //付款计划
//        PrjPayplan payplan = new PrjPayplan();
//        payplan.setZfje(staPayapproval.getBcfkje());
//        payplan.setCjr(staPayapproval.getCjr());
//        payplan.setCjsj(staPayapproval.getCjsj());
//        payplan.setRws(staPayapproval.getRws());
//        payplan.setFysq(staPayapproval.getFysq());
//        payplan.setJhmc(staPayapproval.getFkjhmc());
//        prjPayplanService.insert(payplan);
//        //合同
        PrjContract contract = new PrjContract();
//        contract.setHtmc(staPayapproval.getHtmc());
        contract.setWj(staPayapproval.getHtwj());
        contract.setId(staPayapproval.getHt());
        contract.setYszl(staPayapproval.getYszl());
         prjContractService.update(contract);

        PrjPayplan payplan = new PrjPayplan();
        payplan.setId(staPayapproval.getFkjh());
        payplan.setFysq(staPayapproval.getFysq());
        prjPayplanService.update(payplan);

        // 合同自动更新当前状态
//        Map<String, Object>maps = new HashMap<>();
//        maps.put("id",staPayapproval.getHt());
//        maps.put("spzt","已锁定");
//        prjContractService.updateSpzt(maps);
        return this.staPayapprovalDao.insert(staPayapproval);
    }
    /**
     * 撤销审批状态
     *
     * @param staPayapproval 实例对象
     * @return 实例对象
     */
    @Override
    public int tjrevokeApprove(StaPayapproval staPayapproval) {
        return this.staPayapprovalDao.tjrevokeApprove(staPayapproval);
    }

    /**
     * 修改数据
     *
     * @param staPayapproval 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update(StaPayapproval staPayapproval) {
        //以下内容已单独处理
//        //付款计划
//        PrjPayplan payplan = new PrjPayplan();
//        payplan.setId(staPayapproval.getFkjh());
//        payplan.setZfje(staPayapproval.getBcfkje());
//        prjPayplanService.update(payplan);
//        //合同
        PrjContract contract = new PrjContract();
        contract.setId(staPayapproval.getHt());
        contract.setWj(staPayapproval.getHtwj());
        contract.setHtje(staPayapproval.getHtje());
        contract.setYszl(staPayapproval.getYszl());
        prjContractService.update(contract);

        PrjPayplan payplan = new PrjPayplan();
        payplan.setId(staPayapproval.getFkjh());
        payplan.setFysq(staPayapproval.getFysq());
        prjPayplanService.update(payplan);

        return this.staPayapprovalDao.update(staPayapproval);
    }

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

    @Override
    public List<StaPayapproval> getFksqList(Map map) {
        return this.staPayapprovalDao.getFksqList(map);
    }

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

    @Override
    public int updateSpzt(StaPayapproval staPayapproval) {
        return this.staPayapprovalDao.updateSpzt(staPayapproval);
    }

    @Override
    public List<StaPayapproval> getFksq(Map map) {
        return this.staPayapprovalDao.getFksq(map);
    }

    @Override
    public List<StaPayapproval> queryByHtId(Integer id) {
        return this.staPayapprovalDao.queryByHtId(id);
    }

    @Override
    public StaPayapproval getYFKJE(String fysq) {
        return this.staPayapprovalDao.getYFKJE(fysq);
    }

    @Resource
    private BaseDictDao dictDao;
    @Override
    public List<ViewFileVo> viewfile(String file) {

        List<StaReimbursementVo> staReimbursementVos = JsonTool.parseArray(file, 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");
        AtomicInteger count = new AtomicInteger(0);
        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 List<StaPayapproval> getFkjh(Integer id) {
        return this.staPayapprovalDao.getFkjh(id);
    }

    @Override
    public Double selectPayByFkjh(Integer id) {
        return staPayapprovalDao.selectPayByFkjh(id);
    }
    @Override
    public List<PrjAnnualbudget> getNdfk(Map map) {
        return this.staPayapprovalDao.getNdfk(map);
    }

    @Override
    public void updateHtje(PrjContract prjContract) {
        staPayapprovalDao.updateHtje(prjContract);
    }

    @Override
    public List<PrjAnnualbudget> getXmzys(Map map) {
        return this.staPayapprovalDao.getXmzys(map);
    }

}
