const system = require("../../../system");
const ServiceBase = require("../../sve.base")
class CschemeService extends ServiceBase {
    constructor() {
        super("chance", ServiceBase.getDaoName(CschemeService));
        this.redisClient =system.getObject("util.redisClient");
        this.cclueDao = system.getObject("db.chance.cclueDao");
    }

    /**
     * fn:保存线索方案
     * @param params
     * @returns {Promise<void>}
     */
    async saveCscheme(params){
        if(!params.deliver_id){
            return system.getResult(null, `参数错误 交付商ID不能为空`);
        }
        if(!params.id){
            return system.getResult(null, `参数错误 线索ID不能为空`);
        }
        if(!params.clue_type_name){
            return system.getResult(null, `参数错误 产品名称不能为空`);
        }
        if(!params.name){
            return system.getResult(null, `参数错误 个体户名称不能为空`);
        }
        if(!params.business_place){
            return system.getResult(null, `参数错误 注册地不能为空`);
        }
        if(!params.legal_name){
            return system.getResult(null, `参数错误 法人不能为空`);
        }
        if(!params.legal_mobile){
            return system.getResult(null, `参数错误 法人电话不能为空`);
        }
        if(!params.service_ids){
            return system.getResult(null, `参数错误 服务项不能为空`);
        }
        if(!params.business_type){
            return system.getResult(null, `参数错误 经营类型不能为空`);
        }
        if(!params.businessscope){
            return system.getResult(null, `参数错误 经营范围不能为空`);
        }
        //查询线索是否存在或者是已经关闭
        let cclueBean = await  this.cclueDao.getCclueInfo({id:this.trim(params.id),deliver_id:this.trim(params.deliver_id)});
        if(!cclueBean){
            return system.getResult(null,`线索不存在`);
        }
        if(cclueBean.status=="40"){
            return system.getResult(null, `线索已关闭`);
        }

        let cschemeBean = await this.dao.model.findOne({
            where:{
                id: this.trim(params.id),
                deliver_id: this.trim(params.deliver_id)
            },
            attributes: ['id']
        });
        if(!cschemeBean){
            return system.getResult(null, `线索数据存在异常`);
        }
        try{
            let cschemeProperties ={
                id: this.trim(params.id),
                deliver_id: this.trim(params.deliver_id),
                clue_type_name: this.trim(params.clue_type_name),
                name: this.trim(params.name),
                business_place: this.trim(params.business_place),
                legal_name: this.trim(params.legal_name),
                legal_mobile: this.trim(params.legal_mobile),
                service_ids: this.trim(params.service_ids) ,
                business_type: this.trim(params.business_type),
                businessscope: this.trim(params.businessscope),
                business_type_id:this.trim(params.business_type_id) || "",
                remarks: this.trim(params.remarks) || ""
            };
            await this.db.transaction(async t=>{
                await this.dao.update(cschemeProperties, t);
                let cclueProperties = {
                    id: this.trim(params.id),
                    status: "30"
                };
                await this.cclueDao.update(cclueProperties, t);
            })
            return system.getResultSuccess();
        }catch (e) {
            console.log(e);
            return system.getResult(null,`系统错误`)
        }
    }
}

module.exports = CschemeService;