package thyyxxk.webserver.service.medicalinsurance;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.yaml.snakeyaml.util.UriEncoder;
import thyyxxk.webserver.config.envionment.SystemConfig;
import thyyxxk.webserver.constants.sidicts.SiFunction;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.medicalinsurance.manage.inventory.InvinfoDelete;
import thyyxxk.webserver.entity.medicalinsurance.manage.inventory.InvinfoModify;
import thyyxxk.webserver.entity.medicalinsurance.manage.inventory.InvinfoUpload;
import thyyxxk.webserver.entity.medicalinsurance.manage.inventory.PurcinfoBought;
import thyyxxk.webserver.entity.medicalinsurance.manage.inventory.SelinfoReturn;
import thyyxxk.webserver.entity.medicalinsurance.manage.inventory.SelinfoSold;
import thyyxxk.webserver.entity.medicine.YpCodgDownload;
import thyyxxk.webserver.entity.medicine.YpTracCodg;
import thyyxxk.webserver.utils.SiUtil;
import thyyxxk.webserver.utils.TokenUtil;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class SiInventoryService {
    private final ExecService exec;
    private final SystemConfig sysCfg;

    @Autowired
    public SiInventoryService(ExecService exec, SystemConfig sysCfg) {
        this.exec = exec;
        this.sysCfg = sysCfg;
    }

    public ResultVo<String> uploadInvinfo(List<InvinfoUpload> invinfo) {
        String ref = JSONArray.toJSONStringWithDateFormat(invinfo, "yyyy-MM-dd");
        JSONObject input = exec.makeTradeHeader(SiFunction.UPLOAD_COMMODITY_INVENTORY);
        input.getJSONObject("input").put("invinfoDetail", JSONArray.parseArray(ref));
        JSONObject result = exec.executeTrade(input, SiFunction.UPLOAD_COMMODITY_INVENTORY);
        return SiUtil.makeReturnWithoutOutput(result, "商品盘存上传成功。");
    }

    public ResultVo<JSONObject> modifyInvinfo(List<InvinfoModify> invinfo) {
        String ref = JSONArray.toJSONStringWithDateFormat(invinfo, "yyyy-MM-dd HH:mm:ss");
        JSONObject input = exec.makeTradeHeader(SiFunction.CHANGE_COMMODITY_INVENTORY);
        input.getJSONObject("input").put("invinfoDetail", JSONArray.parseArray(ref));
        JSONObject result = exec.executeTrade(input, SiFunction.CHANGE_COMMODITY_INVENTORY);
        return SiUtil.makeReturnWithoutOutputOfElseInfo(result);
    }

    public ResultVo<JSONObject> modifyInvinfoItem(InvinfoModify invinfo) {
        invinfo.setFixmedinsHilistId(sysCfg.getInstitutionId());
        invinfo.setFixmedinsHilistName(sysCfg.getInstitutionName());
        invinfo.setInvChgTime(new Date());
        String ref = JSONArray.toJSONStringWithDateFormat(invinfo, "yyyy-MM-dd HH:mm:ss");
        JSONObject input = exec.makeTradeHeader(SiFunction.CHANGE_COMMODITY_INVENTORY);
        input.getJSONObject("input").put("invinfo", JSONArray.parseObject(ref));
        JSONObject result = exec.executeTrade(input, SiFunction.CHANGE_COMMODITY_INVENTORY);
        return SiUtil.makeReturnWithoutOutputOfElseInfo(result);
    }

    public ResultVo<String> purchaseGoods(List<PurcinfoBought> purcinfo) {
        String ref = JSONArray.toJSONString(purcinfo);
        JSONObject input = exec.makeTradeHeader(SiFunction.UPLOAD_COMMODITY_PURCHASE);
        input.getJSONObject("input").put("purcinfoDetail", JSONArray.parseArray(ref));
        JSONObject result = exec.executeTrade(input, SiFunction.UPLOAD_COMMODITY_PURCHASE);
        return SiUtil.makeReturnWithoutOutput(result, "商品采购上传成功。");
    }

    public ResultVo<String> returnGoods(List<PurcinfoBought> purcinfo) {
        String ref = JSONArray.toJSONString(purcinfo);
        JSONObject input = exec.makeTradeHeader(SiFunction.UPLOAD_COMMODITY_PURCHASE_RETURN);
        input.getJSONObject("input").put("purcinfoDetail", JSONArray.parseArray(ref));
        JSONObject result = exec.executeTrade(input, SiFunction.UPLOAD_COMMODITY_PURCHASE_RETURN);
        return SiUtil.makeReturnWithoutOutput(result, "商品采购退货成功。");
    }

    public ResultVo<JSONObject> saleGoods(List<SelinfoSold> selinfo) {
        String ref = JSONArray.toJSONString(selinfo);
        JSONObject input = exec.makeTradeHeader(SiFunction.UPLOAD_COMMODITY_SALES);
        input.getJSONObject("input").put("selinfoDetail", JSONArray.parseArray(ref));
        JSONObject result = exec.executeTrade(input, SiFunction.UPLOAD_COMMODITY_SALES);
        return SiUtil.makeReturnWithoutOutputOfElseInfo(result);
    }

    public ResultVo<JSONObject> returnSales(List<SelinfoReturn> selinfo) {
        String ref = JSONArray.toJSONString(selinfo);
        JSONObject input = exec.makeTradeHeader(SiFunction.UPLOAD_COMMODITY_SALES_RETURN);
        input.getJSONObject("input").put("selinfoDetail", JSONArray.parseArray(ref));
        JSONObject result = exec.executeTrade(input, SiFunction.UPLOAD_COMMODITY_SALES_RETURN);
        return SiUtil.makeReturnWithoutOutputOfElseInfo(result);
    }

    /**
     * 商品销售（药品开药上传）
     * @param selinfo
     * @return
     */
    public ResultVo<JSONObject> saleGoodsItem(Map<String, Object> selinfo) {
        log.info("------------saleGoodsItem------------");
        log.info("selinfo=" + JSONObject.toJSONString(selinfo));
//        selinfo.put("fixmedins_hilist_id", sysCfg.getInstitutionId());
//        selinfo.put("fixmedins_hilist_name", sysCfg.getInstitutionName());
        selinfo.put("prsc_dr_name", UriEncoder.decode(String.valueOf(selinfo.get("prsc_dr_name"))));
        selinfo.put("phar_name", UriEncoder.decode(String.valueOf(selinfo.get("phar_name"))));
        selinfo.put("psn_name", UriEncoder.decode(String.valueOf(selinfo.get("psn_name"))));
        selinfo.put("sel_retn_opter_name", UriEncoder.decode(String.valueOf(selinfo.get("sel_retn_opter_name"))));
        selinfo.put("fixmedins_hilist_name", UriEncoder.decode(String.valueOf(selinfo.get("fixmedins_hilist_name"))));
        String ref = JSONObject.toJSONString(selinfo);
        log.info("ref=" + ref);
        JSONObject input = exec.makeTradeHeader(SiFunction.UPLOAD_COMMODITY_SALES);
        input.getJSONObject("input").put("selinfo", JSONObject.parseObject(ref));
        JSONObject result = exec.executeTrade(input, SiFunction.UPLOAD_COMMODITY_SALES);
        return SiUtil.makeReturnWithoutOutputOfElseInfo(result);
    }

    public ResultVo<JSONObject> returnGoodsItem(Map<String, Object> selinfo) {
        log.info("------------returnGoodsItem------------");
        log.info("selinfo=" + JSONObject.toJSONString(selinfo));
        selinfo.put("fixmedins_hilist_id", sysCfg.getInstitutionId());
        selinfo.put("fixmedins_hilist_name", sysCfg.getInstitutionName());
        selinfo.put("sel_retn_opter_name", UriEncoder.decode(String.valueOf(selinfo.get("sel_retn_opter_name"))));
        String ref = JSONObject.toJSONString(selinfo);
        JSONObject input = exec.makeTradeHeader(SiFunction.UPLOAD_COMMODITY_SALES_RETURN);
        input.getJSONObject("input").put("selinfo", JSONObject.parseObject(ref));
        JSONObject result = exec.executeTrade(input, SiFunction.UPLOAD_COMMODITY_SALES_RETURN);
        return SiUtil.makeReturnWithoutOutputOfElseInfo(result);
    }

    public ResultVo<JSONObject> deleteGoods(List<InvinfoDelete> invinfo) {
        String ref = JSONArray.toJSONString(invinfo);
        JSONObject input = exec.makeTradeHeader(SiFunction.DELETE_COMMODITY_INFO);
        input.getJSONObject("input").put("goodDetail", JSONArray.parseArray(ref));
        JSONObject result = exec.executeTrade(input, SiFunction.DELETE_COMMODITY_INFO);
        return SiUtil.makeReturnWithoutOutputOfElseInfo(result);
    }

    public ResultVo<JSONObject> selectSalesGoods(YpTracCodg codg) {
        String ref = JSONObject.toJSONString(codg);
        JSONObject input = exec.makeTradeHeader(SiFunction.SELECT_COMMODITY_SALES);
        input.getJSONObject("input").put("data", JSONObject.parseObject(ref));
        JSONObject result = exec.executeTrade(input, SiFunction.SELECT_COMMODITY_SALES);
        return SiUtil.makeReturnWithoutOutputOfElseInfo(result);
    }

    public ResultVo<JSONObject> selectModifyInvinfoItem(Map<String, Object> codg) {
        log.info("------------selectModifyInvinfoItem------------");
        log.info("selinfo=" + JSONObject.toJSONString(codg));
        codg.put("fixmedins_code", sysCfg.getInstitutionId());
        String ref = JSONObject.toJSONString(codg);
        JSONObject input = exec.makeTradeHeader(SiFunction.SELECT_COMMODITY_INVENTORY);
        input.getJSONObject("input").put("data", JSONObject.parseObject(ref));
        JSONObject result = exec.executeTrade(input, SiFunction.SELECT_COMMODITY_INVENTORY);
        return SiUtil.makeReturnWithoutOutputOfElseInfo(result);
    }

    public ResultVo<JSONObject> selectSalesGoodsItem(Map<String, Object> codg) {
        log.info("------------selectSalesGoodsItem------------");
        log.info("selinfo=" + JSONObject.toJSONString(codg));
        codg.put("fixmedins_code", sysCfg.getInstitutionId());
        String ref = JSONObject.toJSONString(codg);
        JSONObject input = exec.makeTradeHeader(SiFunction.SELECT_COMMODITY_SALES);
        input.getJSONObject("input").put("data", JSONObject.parseObject(ref));
        JSONObject result = exec.executeTrade(input, SiFunction.SELECT_COMMODITY_SALES);
        return SiUtil.makeReturnWithoutOutputOfElseInfo(result);
    }

    public ResultVo<JSONObject> selectDownloadCodgBchno(YpCodgDownload codg) {
        String ref = JSONObject.toJSONString(codg);
        JSONObject input = exec.makeTradeHeader(SiFunction.DOWNLOAD_CODG_BCHNO);
        input.getJSONObject("input").put("data", JSONObject.parseObject(ref));
        JSONObject result = exec.executeTrade(input, SiFunction.DOWNLOAD_CODG_BCHNO);
        return SiUtil.makeReturnWithoutOutputOfElseInfo(result);
    }

}
