package com.sifang.modules.bestsign;

import com.alibaba.fastjson.JSONObject;
import com.sifang.modules.bestsign.vo.BestSignCallBackRes;
import com.sifang.modules.bestsign.vo.BestSignOperationVo;
import com.sifang.modules.sunshine.SystemSetting.service.CrmTraderContractService;
import com.sifang.modules.sunshine.SystemSetting.utils.BusinessException;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sys.web.BaseExceptionController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

//上上签 回调
@Controller
@RequestMapping(value = "${adminPath}/main/bestSignCallBack")
public class BestSignCallBackController extends BaseExceptionController {
    Logger logger= LoggerFactory.getLogger(getClass());

    @Autowired
    private CrmTraderContractService crmTraderContractService;

    //签约回调
    @ResponseBody
    @RequestMapping("/signedPush")
    public Result signedPush(@RequestBody JSONObject json){
       logger.info(">>>>>>>>>>>>>上上签回调  signedPush json:{}",json);
        try {
            String type=json.getString("type");
            logger.info(">>>>>>>>>>>>>上上签回调  signedPush type:{}",type);
            if("CONTRACT_COMPLETE".equals(type)){  //签署完成
                BestSignCallBackRes backRes = JSONObject.parseObject(json.toString(), BestSignCallBackRes.class);
                logger.info(">>>>>>>>>>>>>>>上上签回调 signedPush backRes:{}", backRes.toString());
                crmTraderContractService.signedPush(backRes);
            }else if("OPERATION_COMPLETE".equals(type)){  //合同被收件人签署
                BestSignOperationVo backRes = JSONObject.parseObject(json.toString(), BestSignOperationVo.class);
                logger.info(">>>>>>>>>>>>>>>上上签回调 signedPush OperationVo:{}", backRes.toString());
                crmTraderContractService.operationSignPush(backRes);
            }
        } catch (BusinessException c) {
            c.printStackTrace();
            logger.error(">>>>>>>>>>>>>>>>>>上上签回调 signedPush c:{}", c.getMessage());
            return Result.fail("");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(">>>>>>>>>>>>>>>>>>上上签回调 signedPush e:{}", e);
            return Result.fail("");
        }
        return Result.ok();
    }

}
