/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.workcontent.service;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.projectcontentinfo.entity.ProjectContentData;
import com.jeeplus.modules.workcontent.dao.WorkContentContractTermDao;
import com.jeeplus.modules.workcontent.entity.WorkContentContractTerm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.service.CrudService;
import com.jeeplus.modules.workcontent.entity.WorkContentContractinfo;
import com.jeeplus.modules.workcontent.dao.WorkContentContractinfoDao;

/**
 * 合同登记Service
 * @author ssrh
 * @version 2018-06-12
 */
@Service
@Transactional(readOnly = true)
public class WorkContentContractinfoService extends CrudService<WorkContentContractinfoDao, WorkContentContractinfo> implements WorkContentDetailBaseService{

    @Autowired
    private WorkContentContractTermDao contractTermDao;

	public WorkContentContractinfo get(String id) {
		return super.get(id);
	}
	
	public List<WorkContentContractinfo> findList(WorkContentContractinfo workContentContractinfo) {
		return super.findList(workContentContractinfo);
	}
	
	public Page<WorkContentContractinfo> findPage(Page<WorkContentContractinfo> page, WorkContentContractinfo workContentContractinfo) {
		return super.findPage(page, workContentContractinfo);
	}
	
	@Transactional(readOnly = false)
	public void save(WorkContentContractinfo workContentContractinfo) {
		super.save(workContentContractinfo);
	}
	
	@Transactional(readOnly = false)
	public void delete(WorkContentContractinfo workContentContractinfo) {
		super.delete(workContentContractinfo);
	}

    @Override
    @Transactional(readOnly = false)
    public void saveDetails(String detailStr, String projectId, String contentId) {
        if(StringUtils.isBlank(detailStr)){
            return;
        }
        JSONObject jsonObject = JSON.parseObject(detailStr);
        ProjectContentData program = new ProjectContentData();
        program.setId(jsonObject.getString("program.id"));
        WorkContentContractinfo contractinfo = jsonObject.toJavaObject(WorkContentContractinfo.class);
        contractinfo.setProgram(program);
        contractinfo.setProjectId(projectId);
        contractinfo.setContentId(contentId);
        this.save(contractinfo);
        if(contractinfo.getTermList()==null||contractinfo.getTermList().isEmpty()){
            return;
        }
        for (WorkContentContractTerm contractTerm : contractinfo.getTermList()) {
            if(WorkContentContractTerm.DEL_FLAG_DELETE.equals(contractTerm.getDelFlag())){
                contractTermDao.deleteByLogic(contractTerm);
                continue;
            }
            contractTerm.setContractId(contractinfo.getId());
            if(contractTerm.getIsNewRecord()){
                contractTerm.preInsert();
                contractTermDao.insert(contractTerm);
            }else {
                contractTermDao.update(contractTerm);
            }
        }
    }

    public void queryTerms(WorkContentContractinfo oldRecord) {
        if(oldRecord==null|| StringUtils.isBlank(oldRecord.getId())){
            return;
        }
        WorkContentContractTerm contractTerm = new WorkContentContractTerm();
        contractTerm.setContractId(oldRecord.getId());
        List<WorkContentContractTerm> list = contractTermDao.findList(contractTerm);
        oldRecord.setTermList(list);
    }
}