package nccloud.api.so.saleinvoice.operator;

import com.alibaba.fastjson.JSONObject;
import nc.bs.logging.Logger;
import nccloud.api.rest.utils.ResultMessageUtil;
import nccloud.ws.rest.resource.AbstractNCCRestResource;
import org.json.JSONString;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

/**
 * 销售发票接口适配2005代码
 * zhangxinah
 */
@Path("so/saleinvoice/operator")
public class SaleInvResource extends AbstractNCCRestResource {

    public SaleInvResource() {
    }

    @Override
    public String getModule() {
        return "so";
    }

    @POST
    @Path("saveCommit")
    @Consumes({"application/json"})
    @Produces({"application/json"})
    public JSONString saveCommit(JSONString json) {
        try {
            JSONObject jobject = JSONObject.parseObject(json.toJSONString());
            if (jobject == null) {
                return ResultMessageUtil.exceptionToJSON(new NullPointerException("JSONString:null"));
            }
            JSONObject bject = jobject.getJSONObject("billhead");
            if (bject == null) {
                return ResultMessageUtil.exceptionToJSON(new NullPointerException("billhead:null"));
            }
            IAPISaleInvMaitainImpl service = new IAPISaleInvMaitainImpl();
            return service.saveBlue(jobject);
        } catch (Exception e) {
            Logger.error("saveCommit-exp: ", e);
            return ResultMessageUtil.exceptionToJSON(e);
        }
    }

    /**
     * 销售发票红冲接口
     *
     * @author lj
     * @date 2025/03/14
     * @update 20250415 mzr修改
     */
    @POST
    @Path("billSaveRp")
    @Consumes({"application/json"})
    @Produces({"application/json"})
    public JSONString billSaveRp(JSONString json) {
        try {
            JSONObject jobject = JSONObject.parseObject(json.toJSONString());
            if (jobject == null) {
                return ResultMessageUtil.exceptionToJSON(new NullPointerException("JSONString:null"));
            }
            IAPISaleInvMaitainImpl service = new IAPISaleInvMaitainImpl();
            return service.saveRed(jobject);
        } catch (Exception e) {
            Logger.error("billSaveRp-exp: ", e);
            return ResultMessageUtil.exceptionToJSON(e);
        }
    }

}