package cn.stylefeng.team.project.modular.filing.service.impl;

import cn.stylefeng.team.project.modular.filing.entity.BevProdFilingList;
import cn.stylefeng.team.project.modular.filing.mapper.BevProdFilingListMapper;
import cn.stylefeng.team.project.modular.filing.pojo.aidesign.aiparse.ParseToBusinessRequest;
import cn.stylefeng.team.project.modular.filing.pojo.aidesign.aiparse.ParseToBusinessResult;
import cn.stylefeng.team.project.modular.filing.service.BevProdFilingListService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

@Service
public class BevProdFilingListServiceImpl extends ServiceImpl<BevProdFilingListMapper, BevProdFilingList> implements BevProdFilingListService {

    @Override
    public void updateBusinessFilingInfo(ParseToBusinessResult parseToBusinessResult) {

        if (parseToBusinessResult == null) {
            return;
        }
        ParseToBusinessRequest parseToBusinessRequest = parseToBusinessResult.getParseToBusinessRequest();
        if (parseToBusinessRequest == null) {
            return;
        }
        Long businessId = parseToBusinessRequest.getBusinessId();
        if (businessId == null) {
            return;
        }

        LambdaQueryWrapper<BevProdFilingList> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BevProdFilingList::getMainTitle, parseToBusinessResult.getExcelMainTitle());
        wrapper.eq(BevProdFilingList::getSubTitle, parseToBusinessResult.getExcelSubTitle());
        wrapper.eq(BevProdFilingList::getFilingNo, parseToBusinessResult.getExcelNumber());
        wrapper.eq(BevProdFilingList::getFilingVersion, parseToBusinessResult.getFilingVersion());
        BevProdFilingList bevProdFilingList = this.getOne(wrapper, false);
        if (bevProdFilingList == null) {
            bevProdFilingList = new BevProdFilingList();
            bevProdFilingList.setBusinessId(businessId);
            bevProdFilingList.setMainTitle(parseToBusinessResult.getExcelMainTitle());
            bevProdFilingList.setSubTitle(parseToBusinessResult.getExcelSubTitle());
            bevProdFilingList.setFilingNo(parseToBusinessResult.getExcelNumber());
            bevProdFilingList.setFilingVersion(parseToBusinessResult.getFilingVersion());
            this.save(bevProdFilingList);
        } else {
            bevProdFilingList.setBusinessId(businessId);
            this.updateById(bevProdFilingList);
        }
    }

    @Override
    public BevProdFilingList getBusinessFilingInfo(Long businessId) {
        LambdaQueryWrapper<BevProdFilingList> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BevProdFilingList::getBusinessId, businessId);
        return this.getOne(wrapper, false);
    }

}
