package cn.ezhizao.project.business.technologicalProcessDetailDrawing.service.impl;


import cn.ezhizao.common.utils.SecurityUtils;
import cn.ezhizao.common.utils.pdf.PdfUtils;
import cn.ezhizao.framework.config.RuoYiConfig;
import cn.ezhizao.framework.web.domain.AjaxResult;
import cn.ezhizao.project.business.message.domain.BizMessage;
import cn.ezhizao.project.business.message.service.IBizMessageService;
import cn.ezhizao.project.business.personSeal.domain.BizPersonalSeal;
import cn.ezhizao.project.business.personSeal.mapper.BizPersonalSealMapper;
import cn.ezhizao.project.business.publicSeal.domain.BizPublicSeal;
import cn.ezhizao.project.business.publicSeal.mapper.BizPublicSealMapper;
import cn.ezhizao.project.business.technologicalProcessDetailDrawing.domain.BizTechnologicalProcessDetailDrawing;
import cn.ezhizao.project.business.technologicalProcessDetailDrawing.mapper.BizTechnologicalProcessDetailDrawingMapper;
import cn.ezhizao.project.business.technologicalProcessDetailDrawing.service.IBizTechnologicalProcessDetailDrawingService;
import cn.ezhizao.project.business.technologicalProcessDetailDrawingRecords.domain.BizTechnologicalProcessDetailDrawingRecords;
import cn.ezhizao.project.business.technologicalProcessDetailDrawingRecords.mapper.BizTechnologicalProcessDetailDrawingRecordsMapper;
import cn.ezhizao.project.business.technologicalProcessDetailDrawingUser.domain.BizTechnologicalProcessDetailDrawingUser;
import cn.ezhizao.project.business.technologicalProcessDetailDrawingUser.mapper.BizTechnologicalProcessDetailDrawingUserMapper;
import cn.ezhizao.project.system.domain.SysRole;
import cn.ezhizao.project.system.domain.SysUser;
import cn.ezhizao.project.system.mapper.SysRoleMapper;
import cn.ezhizao.project.system.mapper.SysUserMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itextpdf.text.DocumentException;
import org.apache.pdfbox.exceptions.COSVisitorException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 电子图纸Service业务层处理
 *
 * @author ezhizao
 * @date 2024-05-30
 */
@Service
public class BizTechnologicalProcessDetailDrawingServiceImpl extends ServiceImpl<BizTechnologicalProcessDetailDrawingMapper, BizTechnologicalProcessDetailDrawing> implements IBizTechnologicalProcessDetailDrawingService
{
    @Resource
    private BizTechnologicalProcessDetailDrawingMapper bizTechnologicalProcessDetailDrawingMapper;

    @Resource
    BizTechnologicalProcessDetailDrawingRecordsMapper bizTechnologicalProcessDetailDrawingRecordsMapper;

    @Resource
    BizPersonalSealMapper bizPersonalSealMapper;

    @Resource
    BizPublicSealMapper bizPublicSealService;

    @Resource
    SysUserMapper sysUserMapper;
    @Resource
    SysRoleMapper sysRoleMapper;
    @Resource
    IBizMessageService bizMessageService;

    @Resource
    BizTechnologicalProcessDetailDrawingUserMapper bizTechnologicalProcessDetailDrawingUserMapper;


    /**
     * 查询电子图纸列表
     *
     * @param bizTechnologicalProcessDetailDrawing 电子图纸
     * @return 电子图纸
     */
    @Override
    public List<BizTechnologicalProcessDetailDrawing> getList(BizTechnologicalProcessDetailDrawing bizTechnologicalProcessDetailDrawing)
    {
        return bizTechnologicalProcessDetailDrawingMapper.getList(bizTechnologicalProcessDetailDrawing);
    }

    /**
     * 物理删除
     * @param bizTechnologicalProcessDetailDrawing
     * @return 删除结果
     */
    @Override
    public int physicalDelete(BizTechnologicalProcessDetailDrawing bizTechnologicalProcessDetailDrawing){ return bizTechnologicalProcessDetailDrawingMapper.physicalDelete(bizTechnologicalProcessDetailDrawing); }

    @Override
    public List<BizTechnologicalProcessDetailDrawing> getDrawingList(BizTechnologicalProcessDetailDrawing bizTechnologicalProcessDetailDrawing) {
        return bizTechnologicalProcessDetailDrawingMapper.getDrawingList(bizTechnologicalProcessDetailDrawing);
    }

    @Override
    public List<BizTechnologicalProcessDetailDrawing> getDrawingListByBasics(BizTechnologicalProcessDetailDrawing bizTechnologicalProcessDetailDrawing) {
        return bizTechnologicalProcessDetailDrawingMapper.getDrawingListByBasics(bizTechnologicalProcessDetailDrawing);
    }

    @Override
    public Integer getProcuctDrawing() {
        return bizTechnologicalProcessDetailDrawingMapper.getProcuctDrawing();
    }



    @Transactional
    public int saveData( List<BizTechnologicalProcessDetailDrawing> bizTechnologicalProcessDetailDrawing,String userName, String nickName) throws COSVisitorException, DocumentException, IOException {
        SysUser userParam = new SysUser();
        SysUser user = new SysUser();
        userParam.setUserName(userName);
        userParam.setNickName(nickName);
        List<SysUser> userList = sysUserMapper.getListByUserName(userParam);
        if (userList.isEmpty()){
            return 4;
        }else {
            user = userList.get(0);
        }

        BizPersonalSeal person = new BizPersonalSeal();
        person.setUserId(user.getUserId());
        String filePath = RuoYiConfig.getProfile();
        List<BizPersonalSeal> personList = bizPersonalSealMapper.getList(person);
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        // 定义日期格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
        // 格式化日期
        String formattedDate = currentDate.format(formatter);
        if(personList.isEmpty()){
            return 3;
        }
        String personalSeal = personList.get(0).getUrl().substring("/profile".length());
        for(BizTechnologicalProcessDetailDrawing item: bizTechnologicalProcessDetailDrawing){
            String url = "";
            url = item.getUrl();
            url = url.substring("/profile".length());
            if (item.getStatus() == null){
                // 默认情况下执行新增的代码
                item.setProducerId(user.getUserId());
                item.setProducer(user.getNickName());
                item.setStatus(0);

                item.setPattern("新产品");
                //上传调整尺寸


                PdfUtils.reSized(filePath + url,filePath +url.substring(0, url.length()-4) + "制作" + url.substring(url.length()-4),100,"height");
                addText(filePath +url.substring(0, url.length()-4) + "制作" + url.substring(url.length()-4),filePath+url.substring(0, url.length()-4) + "制作人" + url.substring(url.length()-4),"制定人：",550,75,12,270);
                addText(filePath +url.substring(0, url.length()-4) + "制作人" + url.substring(url.length()-4),filePath+url.substring(0, url.length()-4) + "制作时间" + url.substring(url.length()-4),formattedDate,500,75,12,270);
                addImage(filePath+url.substring(0, url.length()-4) + "制作时间" + url.substring(url.length()-4),filePath+url.substring(0, url.length()-4) + "制作完成" + url.substring(url.length()-4),filePath+personalSeal,515,40,30,30,270,1);
                item.setUrl("/profile/"+url.substring(0, url.length()-4) + "制作完成" + url.substring(url.length()-4));
                bizTechnologicalProcessDetailDrawingMapper.insert(item);
                //添加用户记录
                BizTechnologicalProcessDetailDrawingUser drawingUser = new BizTechnologicalProcessDetailDrawingUser();
                drawingUser.setDrawingId(item.getId());
                drawingUser.setProcessCode(item.getProcessCode());
                drawingUser.setDrawingName(item.getDrawingName());
                drawingUser.setType(0);
                drawingUser.setUserId(user.getUserId());
                drawingUser.setNickName(user.getNickName());
                drawingUser.setProductId(item.getProductId());
                drawingUser.setProductVersion(item.getProductVersion());
                drawingUser.setRecordTime(new Date());
                bizTechnologicalProcessDetailDrawingUserMapper.insert(drawingUser);
                break;
            }else {
                switch (item.getStatus()) {
                    //0：待审核，1待批准
                    case 0:
                        item.setReviewerId(user.getUserId());
                        item.setReviewer(user.getNickName());
                        item.setStatus(1);
                        addText(filePath+ url,filePath +url.substring(0, url.length()-4) + "审核" + url.substring(url.length()-4),"审核人：",470,75,12,270);
                        addText(filePath +url.substring(0, url.length()-4) + "审核" + url.substring(url.length()-4),filePath+url.substring(0, url.length()-4) + "审核时间" + url.substring(url.length()-4),formattedDate,420,75,12,270);
                        addImage(filePath +url.substring(0, url.length()-4) + "审核时间" + url.substring(url.length()-4),filePath +url.substring(0, url.length()-4) + "审核完成" + url.substring(url.length()-4),filePath+personalSeal,435,40,30,30,270,1);
                        item.setUrl("/profile/"+url.substring(0, url.length()-4) + "审核完成" + url.substring(url.length()-4));
                        bizTechnologicalProcessDetailDrawingMapper.updateById(item);
                        //添加用户记录
                        BizTechnologicalProcessDetailDrawingUser drawingUser = new BizTechnologicalProcessDetailDrawingUser();
                        drawingUser.setDrawingId(item.getId());
                        drawingUser.setProcessCode(item.getProcessCode());
                        drawingUser.setDrawingName(item.getDrawingName());
                        drawingUser.setType(1);
                        drawingUser.setUserId(user.getUserId());
                        drawingUser.setNickName(user.getNickName());
                        drawingUser.setProductId(item.getProductId());
                        drawingUser.setProductVersion(item.getProductVersion());
                        drawingUser.setRecordTime(new Date());
                        bizTechnologicalProcessDetailDrawingUserMapper.insert(drawingUser);
                        break;
                    case 1:
                        issuer(item,personalSeal,user);
                        sendMessage();
                        break;
                }
            }

        }

        return 1;
    }


    /**
     * 批准逻辑
     * @param item
     */
    @Transactional
    public void issuer (BizTechnologicalProcessDetailDrawing item,String personalSeal,SysUser user) throws DocumentException, IOException {
        item.setIssuerId(user.getUserId());
        item.setIssuer(user.getNickName());
        item.setStatus(2);
        String url = "";
        url = item.getUrl();
        url = url.substring("/profile".length());
        String filePath = RuoYiConfig.getProfile();
        //查询公共章
        String reviewer = getPublicSeal("受控");
        String D = getPublicSeal("D章");
        String heavy = getPublicSeal("重章");
        String isu = getPublicSeal("发行章");
        String rem = getPublicSeal("废弃章");
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        // 定义日期格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
        // 格式化日期
        String formattedDate = currentDate.format(formatter);

        //替换图纸将旧图纸废弃
        if (item.getParentId()!=0){

            QueryWrapper<BizTechnologicalProcessDetailDrawing> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id", item.getParentId());
            BizTechnologicalProcessDetailDrawing byId = bizTechnologicalProcessDetailDrawingMapper.selectOne(queryWrapper);
//            BizTechnologicalProcessDetailDrawing byId = bizTechnologicalProcessDetailDrawingService.query().eq("id", item.getParentId()).one();
            String urlItem = "";
            urlItem = byId.getUrl();
            urlItem = urlItem.substring("/profile".length());
            byId.setStatus(2);
            byId.setAbandoned(1);
            byId.setVoided(user.getNickName());
            byId.setVoidedId(user.getUserId());
            byId.setPattern("作废产品");
            addImage(filePath+ urlItem,filePath +urlItem.substring(0, urlItem.length()-4) + "zf" + urlItem.substring(urlItem.length()-4),filePath+rem,290,880,40,15,300,0.8F);
            addText(filePath +urlItem.substring(0, urlItem.length()-4) + "zf" + urlItem.substring(urlItem.length()-4),filePath +urlItem.substring(0, urlItem.length()-4) + "zfr" + urlItem.substring(urlItem.length()-4),"作废申请人：",310,75,12,270);
            addText(filePath +urlItem.substring(0, urlItem.length()-4) + "zfr" + urlItem.substring(urlItem.length()-4),filePath+urlItem.substring(0, urlItem.length()-4) + "zfdate" + urlItem.substring(urlItem.length()-4),formattedDate,500,75,12,270);

            addImage(filePath +urlItem.substring(0, urlItem.length()-4) + "zfdate" + urlItem.substring(urlItem.length()-4),filePath +urlItem.substring(0, urlItem.length()-4) + "zfover" + urlItem.substring(urlItem.length()-4),filePath+personalSeal,275,40,30,30,270,1);
            byId.setUrl("/profile/" +urlItem.substring(0, urlItem.length()-4) + "zfover" + urlItem.substring(urlItem.length()-4));
            bizTechnologicalProcessDetailDrawingMapper.updateById(byId);
            BizTechnologicalProcessDetailDrawingUser drawingUser = new BizTechnologicalProcessDetailDrawingUser();
            drawingUser.setDrawingId(byId.getId());
            drawingUser.setProcessCode(byId.getProcessCode());
            drawingUser.setDrawingName(byId.getDrawingName());
            drawingUser.setType(4);
            drawingUser.setUserId(user.getUserId());
            drawingUser.setNickName(user.getNickName());
            drawingUser.setProductId(byId.getProductId());
            drawingUser.setProductVersion(byId.getProductVersion());
            drawingUser.setRecordTime(new Date());
            drawingUser.setAbandoned(1);
            bizTechnologicalProcessDetailDrawingUserMapper.insert(drawingUser);
        }
        //判断盖章逻辑
        if (item.getAbandoned() ==1){
            //加盖废弃章，不需要盖其他章
            addImage(filePath+ url,filePath +url.substring(0, url.length()-4) + "zf" + url.substring(url.length()-4),filePath+rem,290,880,40,15,300,0.8F);
            addText(filePath +url.substring(0, url.length()-4) + "zf" + url.substring(url.length()-4),filePath +url.substring(0, url.length()-4) + "zfr" + url.substring(url.length()-4),"作废批准人：",230,75,12,270);
            addText(filePath +url.substring(0, url.length()-4) + "zfr" + url.substring(url.length()-4),filePath+url.substring(0, url.length()-4) + "zfdate" + url.substring(url.length()-4),formattedDate,180,75,12,270);

            addImage(filePath +url.substring(0, url.length()-4) + "zfdate" + url.substring(url.length()-4),filePath +url.substring(0, url.length()-4) + "zfover" + url.substring(url.length()-4),filePath+personalSeal,195,40,30,30,270,1);
            item.setUrl("/profile/"+url.substring(0, url.length()-4) + "zfover" + url.substring(url.length()-4));
            bizTechnologicalProcessDetailDrawingMapper.updateById(item);
            BizTechnologicalProcessDetailDrawingUser drawingUser = new BizTechnologicalProcessDetailDrawingUser();
            drawingUser.setDrawingId(item.getId());
            drawingUser.setProcessCode(item.getProcessCode());
            drawingUser.setDrawingName(item.getDrawingName());
            drawingUser.setType(4);
            drawingUser.setUserId(user.getUserId());
            drawingUser.setNickName(user.getNickName());
            drawingUser.setProductId(item.getProductId());
            drawingUser.setProductVersion(item.getProductVersion());
            drawingUser.setRecordTime(new Date());
            drawingUser.setAbandoned(1);
            bizTechnologicalProcessDetailDrawingUserMapper.insert(drawingUser);
            return;
        }
        //盖批准和审核章还有批准日期
        addText(filePath+ url,filePath +url.substring(0, url.length()-4) + "pz" + url.substring(url.length()-4),"批准人：",390,75,12,270);
        //批准章
        addImage(filePath +url.substring(0, url.length()-4) + "pz" + url.substring(url.length()-4),filePath +url.substring(0, url.length()-4) + "pzz" + url.substring(url.length()-4),filePath+isu,400,860,60,55,300,0.8F);
        //批准章上的日期
        addText(filePath +url.substring(0, url.length()-4) + "pzz" + url.substring(url.length()-4),filePath +url.substring(0, url.length()-4) + "pzdate" + url.substring(url.length()-4),formattedDate,427,913,8,300);
        //个人章
        addImage(filePath +url.substring(0, url.length()-4) + "pzdate" + url.substring(url.length()-4),filePath +url.substring(0, url.length()-4) + "pzr" + url.substring(url.length()-4),filePath+personalSeal,355,40,30,30,270,1);
        //批准日期
        addText(filePath +url.substring(0, url.length()-4) + "pzr" + url.substring(url.length()-4),filePath +url.substring(0, url.length()-4) + "pzrdate" + url.substring(url.length()-4),formattedDate,340,75,12,270);

        //受控章
        addImage(filePath +url.substring(0, url.length()-4) + "pzrdate" + url.substring(url.length()-4),filePath +url.substring(0, url.length()-4) + "pzover" + url.substring(url.length()-4),filePath+reviewer,465,870,45,28,300,0.8F);
        item.setUrl("/profile/" +url.substring(0, url.length()-4) + "pzover" + url.substring(url.length()-4));
        if (item.getIdentification()){
            //盖重章
            addImage(filePath +url.substring(0, url.length()-4) + "pzover" + url.substring(url.length()-4),filePath +url.substring(0, url.length()-4) + "z" + url.substring(url.length()-4),filePath+heavy,320,870,40,40,300,0.8F);
            item.setUrl("/profile" +url.substring(0, url.length()-4) + "z" + url.substring(url.length()-4));
        }
        if (item.getMarkD() ){
            //因为地址不同判断重章有没有盖到
            //盖D章
            if (!item.getIdentification()){
                addImage(filePath +url.substring(0, url.length()-4) + "pzover" + url.substring(url.length()-4),filePath +url.substring(0, url.length()-4) + "dover" + url.substring(url.length()-4),filePath+D,365,870,40,40,300,0.8F);
            }else {
                addImage(filePath +url.substring(0, url.length()-4) + "z" + url.substring(url.length()-4),filePath +url.substring(0, url.length()-4) + "dover" + url.substring(url.length()-4),filePath+D,365,870,40,40,300,0.8F);
            }
            item.setUrl("/profile/" +url.substring(0, url.length()-4) + "dover" + url.substring(url.length()-4));

        }
        bizTechnologicalProcessDetailDrawingMapper.updateById(item);
        BizTechnologicalProcessDetailDrawingUser drawingUser = new BizTechnologicalProcessDetailDrawingUser();
        drawingUser.setDrawingId(item.getId());
        drawingUser.setProcessCode(item.getProcessCode());
        drawingUser.setDrawingName(item.getDrawingName());
        drawingUser.setType(2);
        drawingUser.setUserId(user.getUserId());
        drawingUser.setNickName(user.getNickName());
        drawingUser.setProductId(item.getProductId());
        drawingUser.setProductVersion(item.getProductVersion());
        drawingUser.setRecordTime(new Date());
        bizTechnologicalProcessDetailDrawingUserMapper.insert(drawingUser);
    }



    /**
     * 公共章查村
     */
    public String getPublicSeal(String code) {


        QueryWrapper<BizPublicSeal> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("code", code);

        // 使用 MyBatis-Plus 的 getOne 方法执行查询
        BizPublicSeal seal = bizPublicSealService.selectOne(queryWrapper);

        // 检查 seal 是否为 null
        if (seal != null && seal.getUrl() != null) {
            // 从查询结果中获取 URL 并进行字符串操作
            return seal.getUrl().substring("/profile".length());
        } else {
            // 如果没有找到记录或 URL 为空，则返回 null 或抛出异常
            return null;
    }
    }





    @Transactional
    public int drawingNG(List<BizTechnologicalProcessDetailDrawing> processDetailDrawing,String userName,String nickName) {
        SysUser userParam = new SysUser();
        SysUser user = new SysUser();
        userParam.setUserName(userName);
        userParam.setNickName(nickName);
        List<SysUser> userList = sysUserMapper.getListByUserName(userParam);
        if (userList.isEmpty()){
            return 4;
        }else {
            user = userList.get(0);
        }

        for(BizTechnologicalProcessDetailDrawing  item: processDetailDrawing){
            item.setStatus(3);
            item.setNgUser(user.getNickName());
            item.setUserNgId(user.getUserId());
            BizTechnologicalProcessDetailDrawingUser drawingUser = new BizTechnologicalProcessDetailDrawingUser();
            drawingUser.setDrawingId(item.getId());
            drawingUser.setProcessCode(item.getProcessCode());
            drawingUser.setDrawingName(item.getDrawingName());
            drawingUser.setType(3);
            drawingUser.setUserId(user.getUserId());
            drawingUser.setNickName(user.getNickName());
            drawingUser.setProductId(item.getProductId());
            drawingUser.setProductVersion(item.getProductVersion());
            drawingUser.setRecordTime(new Date());
            if(item.getAbandoned()==1) {
                item.setAbandoned(0);
                item.setStatus(2);
                drawingUser.setType(2);
            }
            bizTechnologicalProcessDetailDrawingMapper.updateById(item);
            bizTechnologicalProcessDetailDrawingUserMapper.insert(drawingUser);
        }

        return 1;
    }


    @Transactional
    public int drawingRemove(List<BizTechnologicalProcessDetailDrawing> processDetailDrawing,String userName,String nickName) throws DocumentException, IOException {

        SysUser userParam = new SysUser();
        SysUser user = new SysUser();
        userParam.setUserName(userName);
        userParam.setNickName(nickName);
        List<SysUser> userList = sysUserMapper.getListByUserName(userParam);
        if (userList.isEmpty()){
            return 4;
        }else {
            user = userList.get(0);
        }

        BizPersonalSeal person = new BizPersonalSeal();
        person.setUserId(user.getUserId());
        List<BizPersonalSeal> personList = bizPersonalSealMapper.getList(person);
        if(personList.isEmpty()){
            return 3;
        }
        String personalSeal = personList.get(0).getUrl().substring("/profile".length());
        LocalDate currentDate = LocalDate.now();
        // 定义日期格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
        // 格式化日期
        String formattedDate = currentDate.format(formatter);
        for(BizTechnologicalProcessDetailDrawing  item: processDetailDrawing){
            String url = "";
            url = item.getUrl();
            url = url.substring("/profile".length());
            String filePath = RuoYiConfig.getProfile();
            item.setStatus(1);
            item.setAbandoned(1);
//            item.setPattern("作废产品");
            item.setVoided(user.getNickName());
            item.setVoidedId(user.getUserId());

            addText(filePath +url.substring(0, url.length()-4) + url.substring(url.length()-4),filePath +url.substring(0, url.length()-4) + "zfr" + url.substring(url.length()-4),"作废申请人：",310,75,12,270);
            addText(filePath +url.substring(0, url.length()-4) + "zfr" + url.substring(url.length()-4),filePath+url.substring(0, url.length()-4) + "zfdate" + url.substring(url.length()-4),formattedDate,260,75,12,270);

            addImage(filePath +url.substring(0, url.length()-4) + "zfdate" + url.substring(url.length()-4),filePath +url.substring(0, url.length()-4) + "zfover" + url.substring(url.length()-4),filePath+personalSeal,275,40,30,30,270,1);
            item.setUrl("/profile/"+url.substring(0, url.length()-4) + "zfover" + url.substring(url.length()-4));
            bizTechnologicalProcessDetailDrawingMapper.updateById(item);
            BizTechnologicalProcessDetailDrawingUser drawingUser = new BizTechnologicalProcessDetailDrawingUser();
            drawingUser.setDrawingId(item.getId());
            drawingUser.setProcessCode(item.getProcessCode());
            drawingUser.setDrawingName(item.getDrawingName());
            drawingUser.setType(1);
            drawingUser.setAbandoned(1);
            drawingUser.setUserId(user.getUserId());
            drawingUser.setNickName(user.getNickName());
            drawingUser.setProductId(item.getProductId());
            drawingUser.setProductVersion(item.getProductVersion());
            drawingUser.setRecordTime(new Date());
            bizTechnologicalProcessDetailDrawingUserMapper.insert(drawingUser);
        }

        return 1;
    }



    @Transactional
    public int replaceDrawing( BizTechnologicalProcessDetailDrawing processDetailDrawing) throws COSVisitorException, DocumentException, IOException {
        //根据parentId插叙旧图纸
        QueryWrapper<BizTechnologicalProcessDetailDrawing> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", processDetailDrawing.getParentId());
        BizTechnologicalProcessDetailDrawing id = bizTechnologicalProcessDetailDrawingMapper.selectOne(queryWrapper);
//        id.setStatus(1);
//        id.setAbandoned(1);
//        id.setPattern("作废产品");
//        id.setVoided(user.getNickName());
//        id.setVoidedId(user.getUserId());
//        bizTechnologicalProcessDetailDrawingService.updateById(id);

// 获取当前日期
        LocalDate currentDate = LocalDate.now();
        // 定义日期格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
        // 格式化日期
        String formattedDate = currentDate.format(formatter);

        BizPersonalSeal person = new BizPersonalSeal();
        person.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        String filePath = RuoYiConfig.getProfile();
        List<BizPersonalSeal> personList = bizPersonalSealMapper.getList(person);
        if(personList.isEmpty()){
            return 3;
        }
        String personalSeal = personList.get(0).getUrl().substring("/profile".length());
        processDetailDrawing.setProducerId(SecurityUtils.getLoginUser().getUser().getUserId());
        processDetailDrawing.setProducer(SecurityUtils.getLoginUser().getUser().getNickName());
        String url = "";
        url = processDetailDrawing.getUrl();
        url = url.substring("/profile".length());

//        processDetailDrawing.setMarkD(id.getMarkD());
//        processDetailDrawing.setIdentification(id.getIdentification());
        processDetailDrawing.setDrawingName(id.getDrawingName());
        processDetailDrawing.setPattern("替换产品");
        processDetailDrawing.setProducerId(SecurityUtils.getLoginUser().getUser().getUserId());
        processDetailDrawing.setProducer(SecurityUtils.getLoginUser().getUser().getNickName());
        processDetailDrawing.setProductId(id.getProductId());
        processDetailDrawing.setProcessCode(id.getProcessCode());
        processDetailDrawing.setProductVersion(id.getProductVersion());
        PdfUtils.reSized(filePath + url,filePath +url.substring(0, url.length()-4) + "制作" + url.substring(url.length()-4),100,"height");
//        addText(filePath +url.substring(0, url.length()-4) + "制作" + url.substring(url.length()-4),filePath+url.substring(0, url.length()-4) + "制作人" + url.substring(url.length()-4),"制定人：",550,75,12,270);
//        addImage(filePath+url.substring(0, url.length()-4) + "制作人" + url.substring(url.length()-4),filePath+url.substring(0, url.length()-4) + "制作完成" + url.substring(url.length()-4),filePath+personalSeal,530,30,45,15,270,1);
        addText(filePath +url.substring(0, url.length()-4) + "制作" + url.substring(url.length()-4),filePath+url.substring(0, url.length()-4) + "制作人" + url.substring(url.length()-4),"制定人：",550,75,12,270);
        addText(filePath +url.substring(0, url.length()-4) + "制作人" + url.substring(url.length()-4),filePath+url.substring(0, url.length()-4) + "制作时间" + url.substring(url.length()-4),formattedDate,500,75,12,270);
        addImage(filePath+url.substring(0, url.length()-4) + "制作时间" + url.substring(url.length()-4),filePath+url.substring(0, url.length()-4) + "制作完成" + url.substring(url.length()-4),filePath+personalSeal,515,40,30,30,270,1);

        processDetailDrawing.setUrl("/profile/"+url.substring(0, url.length()-4) + "制作完成" + url.substring(url.length()-4));

        return bizTechnologicalProcessDetailDrawingMapper.insert(processDetailDrawing);

    }

    @Transactional
    public int edit(@RequestBody BizTechnologicalProcessDetailDrawing processDetailDrawing) {
        QueryWrapper<BizTechnologicalProcessDetailDrawing> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", processDetailDrawing.getId());
        BizTechnologicalProcessDetailDrawing bizTechnologicalProcessDetailDrawing = bizTechnologicalProcessDetailDrawingMapper.selectOne(queryWrapper);
        //进行保存操作记录
        BizTechnologicalProcessDetailDrawingRecords drawingRecords = new BizTechnologicalProcessDetailDrawingRecords();
        drawingRecords.setTechnologicalProcessDetailDrawingId(bizTechnologicalProcessDetailDrawing.getId());
        drawingRecords.setProcessCode(bizTechnologicalProcessDetailDrawing.getProcessCode());
        drawingRecords.setProductId(bizTechnologicalProcessDetailDrawing.getProductId());
        drawingRecords.setProductVersion(bizTechnologicalProcessDetailDrawing.getProductVersion());
        drawingRecords.setUserId(SecurityUtils.getUserId());
        drawingRecords.setType("覆盖");
        drawingRecords.setDrawingName(bizTechnologicalProcessDetailDrawing.getDrawingName());
        bizTechnologicalProcessDetailDrawingRecordsMapper.insert(drawingRecords);
        //覆盖操作
        BizTechnologicalProcessDetailDrawing item = new BizTechnologicalProcessDetailDrawing();
        item.setUrl(processDetailDrawing.getUrl());
        item.setTechnologicalProcessDetailId(bizTechnologicalProcessDetailDrawing.getTechnologicalProcessDetailId());
        item.setDrawingName(bizTechnologicalProcessDetailDrawing.getDrawingName());
        item.setProcessCode(bizTechnologicalProcessDetailDrawing.getProcessCode());
        item.setProductId(bizTechnologicalProcessDetailDrawing.getProductId());
        item.setProductVersion(bizTechnologicalProcessDetailDrawing.getProductVersion());
        bizTechnologicalProcessDetailDrawingMapper.deleteById(bizTechnologicalProcessDetailDrawing.getId());


        return bizTechnologicalProcessDetailDrawingMapper.insert(item);
    }



    /**
     * 放置图片
     * @param srcPath
     * @param resultPath
     * @param imgPath
     * @param x
     * @param y
     * @param width
     * @param height
     * @param rotation
     * @throws DocumentException
     * @throws IOException
     */
    public void addImage(String srcPath,String resultPath,String imgPath,Integer x,Integer y,Integer width,Integer height,Integer rotation,float opacity) throws DocumentException, IOException {
        ByteArrayOutputStream bos = PdfUtils.addImage(srcPath, imgPath, x, y, width, height,rotation, opacity);
        FileOutputStream out = new FileOutputStream(resultPath);
        out.write(bos.toByteArray());
        out.close();
    }

    /**
     * 放置文字
     * @param srcPath
     * @param resultPath
     * @param text
     * @param x
     * @param y
     * @param fontSize
     * @param rotation
     */
    public void addText(String srcPath,String resultPath,String text,Integer x,Integer y,Integer fontSize,Integer rotation) throws IOException, DocumentException {
        ByteArrayOutputStream bos = PdfUtils.addText(srcPath, text, fontSize,x, y,rotation);
        FileOutputStream out = new FileOutputStream(resultPath);
        out.write(bos.toByteArray());
        out.close();
    }

    private void sendMessage() {
        SysRole role = new SysRole();
        role.setRoleKey("drawingRatify");
        List<SysRole> roles = sysRoleMapper.selectRoleList(role);
        List<BizMessage> messages = new ArrayList<>();
        roles.forEach(l -> {
            SysUser conditions = new SysUser();
            conditions.setRoleId(l.getRoleId());
            List<SysUser> items = sysUserMapper.selectAllocatedList(conditions);
            items.forEach(v -> {
                BizMessage message = new BizMessage();
                message.setFromNickName(SecurityUtils.getLoginUser().getUser().getNickName());
                message.setFromUserId(SecurityUtils.getLoginUser().getUser().getUserId());
                message.setFromUserName(SecurityUtils.getLoginUser().getUser().getUserName());
                message.setToNickName(v.getNickName());
                message.setToUserId(v.getUserId());
                message.setToUserName(v.getUserName());
                message.setMessage("您有图纸待批准，请前往【工艺管理】下【等待批准图纸】菜单进行操作。");
                message.setMessageDate(new Date());
                messages.add(message);
            });
        });
        bizMessageService.saveBatch(messages);
    }
}
