package com.yem.wm.syn.eas.api.service.impl;

import com.yem.wm.syn.eas.api.dto.AccessoryAddNewDTO;
import com.yem.wm.syn.eas.api.dto.BillTypeAddNewDTO;
import com.yem.wm.syn.eas.api.service.IBillTypeService;
import com.yem.wm.syn.eas.api.util.OperationServicUtil;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.metadata.dynamicobject.DynamicObjectType;
import kd.bos.exception.KDBizException;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.DeleteServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

import java.util.Date;
import java.util.List;

public class BillTypeServiceImpl implements IBillTypeService {
    @Override
    public Object addNew(BillTypeAddNewDTO billTypeAddNewDTO) {
        String number = billTypeAddNewDTO.getNumber();
        DynamicObject pry = BusinessDataServiceHelper.loadSingle("yem_pricelibrary", new QFilter[]{new QFilter("billno", QCP.equals, number)});
        if (pry!=null){
            throw new KDBizException("已存在编码为:"+number+"的价格库!");
        }
        //审核时间
        Date auditdate = billTypeAddNewDTO.getAuditdate();
        DynamicObject pricelibrary = BusinessDataServiceHelper.newDynamicObject("yem_pricelibrary");
        //编码
        pricelibrary.set("billno",number);
        pricelibrary.set("yem_easid",billTypeAddNewDTO.getEasId());
        //变更状态默认A
        pricelibrary.set("changestatus","A");
        pricelibrary.set("yem_version","1");
        //询价单编码
        String inquiryNum = billTypeAddNewDTO.getInquiryNum();
        DynamicObject inquirylist = BusinessDataServiceHelper.loadSingle("yem_im_inquirylist", new QFilter[]{new QFilter("billno", QCP.equals, inquiryNum)});
        if (inquirylist==null){
            throw new KDBizException("不存在编码为:"+inquiryNum+"的询价单!");
        }
        pricelibrary.set("yem_inquiryNum",inquirylist);
        pricelibrary.set("yem_enquirymessage",inquirylist.getString("yem_enquirymessage"));
        pricelibrary.set("yem_productclass",inquirylist.getDynamicObject("yem_bd_products"));
        //单据类型
        DynamicObject bos_billtype = BusinessDataServiceHelper.loadSingle("bos_billtype", new QFilter[]{new QFilter("number", QCP.equals, "yem_pricelibrary_PJ")});
        pricelibrary.set("yem_billtype", bos_billtype);
        //单据状态
        //组织
        String org1 = billTypeAddNewDTO.getOrg();
        DynamicObject org = BusinessDataServiceHelper.loadSingle("bos_adminorg", new QFilter[]{new QFilter("number", QCP.equals, org1)});
        pricelibrary.set("org",org);
        //报价币别
        DynamicObject currency = BusinessDataServiceHelper.loadSingle("bd_currency", new QFilter[]{new QFilter("number", QCP.equals, "BB01")});
        pricelibrary.set("yem_quotecurrency",currency);
        //创建人
        String creator = billTypeAddNewDTO.getCreator();
        DynamicObject user = BusinessDataServiceHelper.loadSingle("bos_user", new QFilter[]{new QFilter("number", QCP.equals, creator)});
        if (user==null) {
            throw new KDBizException("未找到编码为:"+creator+"的创建人!");
        }
        pricelibrary.set("creator",user);
        //创建时间
        Date createtime = billTypeAddNewDTO.getCreatetime();
        pricelibrary.set("createtime",createtime);
        //业务日期
        Date bizdate = billTypeAddNewDTO.getBizdate();
        pricelibrary.set("yem_bizdate", bizdate);

//        pricelibrary.set("yem_bizauditdate", auditdate);

        List<AccessoryAddNewDTO> entrys = billTypeAddNewDTO.getList();
        DynamicObjectCollection yemAccessory = pricelibrary.getDynamicObjectCollection("yem_accessory");
        DynamicObjectType type = yemAccessory.getDynamicObjectType();
        for (AccessoryAddNewDTO accessory :  entrys) {
            DynamicObject entry = new DynamicObject(type);
            //物料编码
            String materialNum = accessory.getMaterial();
            DynamicObject material = BusinessDataServiceHelper.loadSingle("bd_material", new QFilter[]{new QFilter("number", QCP.equals, materialNum)});
            if (material==null){
                throw new KDBizException("未找到编码为:"+materialNum+"的物料!");
            }
            entry.set("yem_easentryid",accessory.getEntryID());
            entry.set("yem_material",material);
            //加价分类
            entry.set("yem_markup",accessory.getMarkup());
            //标准销售价
            entry.set("yem_standsalesprice",accessory.getStandsalesprice());
            yemAccessory.add(entry);
        }
        pricelibrary.set("billstatus","A");
        SaveServiceHelper.save(new DynamicObject[]{pricelibrary});
        //OperationServicUtil.executeOperate("save",new DynamicObject[]{pricelibrary},"保存失败");
        DynamicObject prices =
                BusinessDataServiceHelper.loadSingle(pricelibrary.getPkValue(), pricelibrary.getDynamicObjectType());

        OperationServicUtil.executeOperate("submit",new DynamicObject[]{prices},"提交失败");

        DynamicObject priceli =
                BusinessDataServiceHelper.loadSingle(pricelibrary.getPkValue(), pricelibrary.getDynamicObjectType());

        OperationServicUtil.executeOperate("audit",new DynamicObject[]{priceli},"审核失败");

        DynamicObject pricelis =
                BusinessDataServiceHelper.loadSingle(pricelibrary.getPkValue(), pricelibrary.getDynamicObjectType());
        //审核人
        String auditorNum = billTypeAddNewDTO.getAuditor();
        DynamicObject auditor = BusinessDataServiceHelper.loadSingle("bos_user", new QFilter[]{new QFilter("number", QCP.equals, auditorNum)});
        if (auditor==null) {
            DeleteServiceHelper.delete(pricelis.getDataEntityType(),new Object[]{pricelis.getPkValue()});
            throw new KDBizException("未找到编码为:"+auditorNum+"的审核人!");
        }
        pricelis.set("auditor",auditor);
        pricelis.set("modifier",auditor);

        pricelis.set("auditdate",auditdate);
        SaveServiceHelper.update(pricelis);
        return null;
    }
}
