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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.service.CrudService;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.projectcontentinfo.entity.ProjectContentData;
import com.jeeplus.modules.workcontent.dao.WorkContentContractSumDao;
import com.jeeplus.modules.workcontent.entity.WorkContentContractSum;
import com.jeeplus.modules.workcontent.entity.WorkContentContractinfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 合同动态汇总Service
 */
@Service
@Transactional(readOnly = true)
public class WorkContentContractSumService extends CrudService<WorkContentContractSumDao, WorkContentContractSum> implements WorkContentDetailBaseService{

	@Autowired
	private WorkContentContractinfoService workContentContractinfoService;


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


    @Transactional(readOnly = false)
    public void saveDetails(WorkContentContractinfo contractinfo) {
        WorkContentContractSum workContentContractSum = new WorkContentContractSum();
        workContentContractSum.setContractinfo(contractinfo);
        double trueAmt = contractinfo.getTrueAmt();
        workContentContractSum.setDynamicCost(trueAmt);
        workContentContractSum.setContractId(contractinfo.getId());
//        workContentContractSum.setWithContractAmount(0.00);
//        workContentContractSum.setWithTargetCost(0.00);
        this.save(workContentContractSum);
    }

	@Override
	public void saveDetails(String detailStr, String projectId, String contentId) {
		if(StringUtils.isBlank(detailStr)){
			return;
		}
		WorkContentContractSum workContentContractSum = new WorkContentContractSum();
		JSONObject jsonObject = JSON.parseObject(detailStr);
		WorkContentContractinfo contractinfo = jsonObject.toJavaObject(WorkContentContractinfo.class);
		String cnumber = contractinfo.getCnumber();
		List<WorkContentContractinfo> byType = workContentContractinfoService.findByType(cnumber);
		workContentContractSum.setContractId(byType.get(0).getId());
		workContentContractSum.setProjectId(projectId);
		workContentContractSum.setContentId(contentId);
		this.save(workContentContractSum);

	}
}