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 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.util.List;

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

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

    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);
        log.info("【操作员：{}】，商品盘存上传：\n参数：{}，\n结果：{}", TokenUtil.getInstance().getTokenUserId(), input, result);
        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);
        log.info("【操作员：{}】，商品盘存修改：\n参数：{}，\n结果：{}", TokenUtil.getInstance().getTokenUserId(), input, result);
        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);
        log.info("【操作员：{}】，商品采购上传：\n参数：{}，\n结果：{}", TokenUtil.getInstance().getTokenUserId(), input, result);
        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);
        log.info("【操作员：{}】，商品采购退货：\n参数：{}，\n结果：{}", TokenUtil.getInstance().getTokenUserId(), input, result);
        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);
        log.info("【操作员：{}】，商品销售上传：\n参数：{}，\n结果：{}", TokenUtil.getInstance().getTokenUserId(), input, result);
        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);
        log.info("【操作员：{}】，商品销售退货：\n参数：{}，\n结果：{}", TokenUtil.getInstance().getTokenUserId(), input, result);
        return SiUtil.makeReturnWithoutOutputOfElseInfo(result);
    }

    public ResultVo<String> 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);
        log.info("【操作员：{}】，商品删除：\n参数：{}，\n结果：{}", TokenUtil.getInstance().getTokenUserId(), input, result);
        return SiUtil.makeReturnWithoutOutput(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);
        log.info("【操作员：{}】，查询商品销售信息：\n参数：{}，\n结果：{}", TokenUtil.getInstance().getTokenUserId(), input, result);
        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);
        log.info("【操作员：{}】，下载企业追溯码信息：\n参数：{}，\n结果：{}", TokenUtil.getInstance().getTokenUserId(), input, result);
        return SiUtil.makeReturnWithoutOutputOfElseInfo(result);
    }

}
