package com.usky.oa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usky.common.core.exception.BusinessException;
import com.usky.common.security.utils.SecurityUtils;
import com.usky.oa.domain.OaCgdDocument;
import com.usky.oa.domain.OaDocument;
import com.usky.oa.domain.OaFormDefinition;
import com.usky.oa.mapper.OaCgdDocumentMapper;
import com.usky.oa.mapper.OaDocumentMapper;
import com.usky.oa.mapper.OaFormDefinitionMapper;
import com.usky.oa.service.OaCgdDocumentService;
import com.usky.common.mybatis.core.AbstractCrudService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * <p>
 * 特殊用品采购单表 服务实现类
 * </p>
 *
 * @author fu
 * @since 2025-02-14
 */
@Service
public class OaCgdDocumentServiceImpl extends AbstractCrudService<OaCgdDocumentMapper, OaCgdDocument> implements OaCgdDocumentService {

    @Autowired
    private OaCgdDocumentMapper oaCgdDocumentMapper;

    @Autowired
    private OaFormDefinitionMapper oaFormDefinitionMapper;

    @Autowired
    private OaDocumentMapper oaDocumentMapper;


    @Override
    public void add(OaCgdDocument oaCgdDocument) {
        if (StringUtils.isBlank(oaCgdDocument.getPurchaseDetails())){
            throw new BusinessException("采购明细不能为空！");
        } else if (StringUtils.isBlank(oaCgdDocument.getReason())){
            throw new BusinessException("采购原因不能为空！");
        } else if (StringUtils.isBlank(oaCgdDocument.getSupplier())){
            throw new BusinessException("供应商不能为空！");
        } else if (oaCgdDocument.getArrivalTime() == null){
            throw new BusinessException("期望到货时间不能为空！");
        } else if (oaCgdDocument.getPaymentMethod() == null){
            throw new BusinessException("支付方式不能为空！");
        }

        String username = SecurityUtils.getUsername();
        Long userId = SecurityUtils.getUserId();
        Long deptId = SecurityUtils.getLoginUser().getSysUser().getDeptId();
        Integer tenantId = SecurityUtils.getTenantId();
        LocalDateTime now = LocalDateTime.now();

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyMMddHHmmss");
        String formattedDate = now.format(formatter);

        LambdaQueryWrapper<OaFormDefinition> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(OaFormDefinition::getFormSign).eq(OaFormDefinition::getId, oaCgdDocument.getFormId());
        OaFormDefinition oaFormDefinition = oaFormDefinitionMapper.selectOne(queryWrapper);
        String sign = oaFormDefinition.getFormSign();
        String docNo = sign + "-" + formattedDate;

        oaCgdDocument.setDocNo(docNo);
        oaCgdDocument.setCreateBy(username);
        oaCgdDocument.setCreateTime(now);
        oaCgdDocument.setProposer(userId);
        oaCgdDocument.setDeptId(deptId);
        oaCgdDocument.setTenantId(tenantId);
        oaCgdDocumentMapper.insert(oaCgdDocument);

        // 插入单据表数据
        OaDocument oaDocument = new OaDocument();
        oaDocument.setFormId(oaCgdDocument.getFormId());
        oaDocument.setType(sign);
        oaDocument.setDocNo(docNo);
        oaDocument.setProposer(userId);
        oaDocument.setDocStatus(oaCgdDocument.getDocStatus());
        oaDocument.setCreateBy(username);
        oaDocument.setCreateTime(oaCgdDocument.getCreateTime());
        oaDocument.setDeptId(deptId);
        oaDocument.setTenantId(tenantId);
        oaDocumentMapper.insert(oaDocument);

    }

    @Override
    public void update(OaCgdDocument oaCgdDocument) {

    }
}
