/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.security.trans.applyAndReport.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import org.security.trans.applyAndReport.dto.WorkOvertimeReportingDTO;
import org.security.trans.applyAndReport.entity.WorkOvertimeReporting;
import org.security.trans.applyAndReport.entity.WorkOvertimeReportingLines;
import org.security.trans.applyAndReport.service.IWorkOvertimeReportingLinesService;
import org.security.trans.applyAndReport.vo.WorkOvertimeReportingVO;
import org.security.trans.applyAndReport.mapper.WorkOvertimeReportingMapper;
import org.security.trans.applyAndReport.service.IWorkOvertimeReportingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.security.trans.flow.dto.FlowStartDTO;
import org.security.trans.flow.service.IFlowHistoryService;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import org.springblade.core.mp.support.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 加班记录报备表 服务实现类
 *
 * @author BladeX
 * @since 2022-07-13
 */
@Service
@AllArgsConstructor
public class WorkOvertimeReportingServiceImpl extends ServiceImpl<WorkOvertimeReportingMapper, WorkOvertimeReporting> implements IWorkOvertimeReportingService {
	private final IWorkOvertimeReportingLinesService workOvertimeReportingLinesService;
	private final IFlowHistoryService flowHistoryService;

	@Override
	public IPage<WorkOvertimeReportingVO> selectWorkOvertimeReportingPage(WorkOvertimeReportingDTO workOvertimeReportingDTO, Query query) {
		IPage<WorkOvertimeReporting> page = Condition.getPage(query);
		QueryWrapper<WorkOvertimeReporting> queryWrapper = Condition.getQueryWrapper(BeanUtil.copyProperties(workOvertimeReportingDTO, WorkOvertimeReporting.class));
		IPage<WorkOvertimeReporting> iPage = this.page(page, queryWrapper);
		IPage<WorkOvertimeReportingVO> res = new Page<>();
		res.setTotal(iPage.getTotal());
		res.setSize(iPage.getSize());
		res.setCurrent(iPage.getCurrent());
		res.setRecords(BeanUtil.copyProperties(iPage.getRecords(), WorkOvertimeReportingVO.class));
		return res;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean saveOrUpdateWorkOvertime(WorkOvertimeReportingDTO workOvertimeReportingDTO){
		WorkOvertimeReporting workOvertimeReporting = Func.copy(workOvertimeReportingDTO, WorkOvertimeReporting.class);
		workOvertimeReporting.setState("0");
		this.saveOrUpdate(workOvertimeReporting);
		//明细表信息
		List<WorkOvertimeReportingLines> workOvertimeLinesList = workOvertimeReportingDTO.getWorkOvertimeLinesList();
		if (Func.isNotEmpty(workOvertimeLinesList)) {
			workOvertimeLinesList.forEach(item -> {
				item.setFormId(workOvertimeReporting.getId());
			});
			//更新明细表数据
			workOvertimeReportingLinesService.saveOrUpdateBatch(workOvertimeLinesList);
		}
		return this.saveOrUpdate(workOvertimeReporting);
	}


	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean handin(WorkOvertimeReportingDTO workOvertimeReportingDTO){
		WorkOvertimeReporting workOvertimeReporting = Func.copy(workOvertimeReportingDTO, WorkOvertimeReporting.class);
		workOvertimeReporting.setState("1");
		this.saveOrUpdate(workOvertimeReporting);
		//明细表信息
		List<WorkOvertimeReportingLines> workOvertimeLinesList = workOvertimeReportingDTO.getWorkOvertimeLinesList();
		if (Func.isNotEmpty(workOvertimeLinesList)) {
			workOvertimeLinesList.forEach(item -> {
				item.setFormId(workOvertimeReporting.getId());
			});
			//更新明细表数据
			workOvertimeReportingLinesService.saveOrUpdateBatch(workOvertimeLinesList);
		}
		//	提交时挂载流程
		FlowStartDTO flowStartDTO = new FlowStartDTO();
		flowStartDTO.setBusinessLinkId(workOvertimeReporting.getId());
		flowStartDTO.setFlowBusinessCode("work_overtime");
		flowHistoryService.startFlow(flowStartDTO);
		return this.saveOrUpdate(workOvertimeReporting);
	}

	@Override
	public WorkOvertimeReportingVO getDetailById(String id){
		WorkOvertimeReporting workOvertimeReporting=super.getById(id);
		WorkOvertimeReportingVO workOvertimeReportingVO = Func.copy(workOvertimeReporting,WorkOvertimeReportingVO.class);
		//获取明细信息
		List<WorkOvertimeReportingLines> lineList = workOvertimeReportingLinesService.list(new QueryWrapper<WorkOvertimeReportingLines>().eq("FORM_ID", id));
		if(Func.isNotEmpty(lineList)){
			workOvertimeReportingVO.setWorkOvertimeLinesList(lineList);
		}
		return workOvertimeReportingVO;
	}

}
