package com.sifang.modules.setting.service;

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.setting.dao.EntryProcessPartnerSetDao;
import com.sifang.modules.setting.dao.EntryProcessSetDao;
import com.sifang.modules.setting.entity.EntryProcessPartnerSet;
import com.sifang.modules.setting.entity.EntryProcessSet;
import com.sifang.modules.setting.req.EditProcessSetReq;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sys.common.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author DQChen
 * @date 2024-02-02 16:20
 */
@Service
public class EntryProcessSetService extends BaseService {

    @Autowired
    EntryProcessSetDao processSetDao;

    @Autowired
    EntryProcessPartnerSetDao processPartnerSetDao;


    /**
     * 根据资方id获取资方设置的建站流程信息数据
     * @param partnerId
     */
    public List<Record> selectSetForPartner(String partnerId) {
        return processSetDao.selectProcessSetListForPartner(partnerId);
    }

    /**
     * 修改资方设置是否启用
     * @param req
     * @return
     */
    public Result editProcessSet(EditProcessSetReq req) {
        EntryProcessSet entryProcessSet = processSetDao.get(req.getProcessSetId());
        EntryProcessPartnerSet partnerSet = processPartnerSetDao.findByProcessSetId(req.getPartnerId(), req.getProcessSetId());
        if (partnerSet == null) {
            partnerSet.setProcessSetId(req.getProcessSetId());
            partnerSet.setPartner(req.getPartnerId());
            partnerSet.setCreateTime(new Date());
            partnerSet.setMustFlag(entryProcessSet.getMustFlag());
            partnerSet.setStatus(req.getStatus());
            processPartnerSetDao.save(partnerSet);
        } else {
            partnerSet.setStatus(req.getStatus());
            processPartnerSetDao.save(partnerSet);
        }
        return Result.ok();
    }

}
