/*
 *      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.basic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.security.common.Constant;
import org.security.ht.vo.common.UploadVO;
import org.security.trans.basic.dto.LawDTO;
import org.security.ht.entity.basic.Department;
import org.security.ht.entity.basic.Law;
import org.security.trans.basic.service.IDepartmentService;
import org.security.ht.vo.basic.LawVO;
import org.security.trans.basic.mapper.LawMapper;
import org.security.trans.basic.service.ILawService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.security.trans.common.service.IFileService;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.Func;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.math.BigDecimal;
import java.util.List;

import static org.security.common.Constant.DELETE_FLAG_0;

/**
 *  服务实现类
 *
 * @author BladeX
 * @since 2022-05-30
 */
@Service
@AllArgsConstructor
public class LawServiceImpl extends ServiceImpl<LawMapper, Law> implements ILawService {
	@Autowired
	private IDepartmentService departmentService;
	private final IFileService fileService;

	@Override
	public IPage<LawVO> selectLawPage(IPage<LawDTO> page, LawDTO lawDTO) {
		String deptId = null;
		if (StringUtils.isNotBlank(lawDTO.getDeptId())) {
			List<Long> deptIds = Func.toLongList(lawDTO.getDeptId());
			deptId = String.valueOf(deptIds.get(deptIds.size() - 1));
		}
		Page<Law> lawPage = new Page<>(page.getCurrent(), page.getSize());
		LambdaQueryWrapper<Law> lawQueryWrapper = setQueryWrappers(lawDTO, deptId);
		IPage<LawVO> resultPage = this.baseMapper.selectLawPage(lawPage, lawQueryWrapper);
		return resultPage;
	}

	/**
	 *构造查询条件
	 */
	private LambdaQueryWrapper<Law> setQueryWrappers(LawDTO lawDTO, String deptId) {
		return new QueryWrapper<Law>().lambda()
			.eq(Law::getDelFlag,DELETE_FLAG_0)
			.eq(StringUtils.isNotBlank(deptId), Law::getDeptId, deptId)
			.like(StringUtils.isNotBlank(lawDTO.getName()), Law::getName, lawDTO.getName())
			.eq(Func.isNoneBlank(lawDTO.getType()),Law:: getType, lawDTO.getType())
			.eq(Func.isNoneBlank(lawDTO.getLawType()),Law:: getLawType, lawDTO.getLawType())
			.orderByDesc(Law::getCreatedTime);
	}

	@Override
	public LawVO getLawById(BigDecimal id) {
		Law law = this.getById(id);
		Department department = departmentService.getById(law.getDeptId());
		LawVO lawVO = Func.copy(law, LawVO.class);
		assert lawVO != null;
		lawVO.setDeptName(department.getDeptName());
		//获取相关法律法规附件
		List<UploadVO> fileList = fileService.getAttachments(String.valueOf(id),law.getGroupName());
		lawVO.setFileList(fileList);
		return lawVO;
	}

	@Override
	public R saveLaw(LawDTO lawDTO){
		lawDTO.setState(Constant.FORM_STATE_DRAFT);
		super.saveOrUpdate(lawDTO);
		//保存相关附件信息
		fileService.saveAttachments(lawDTO.getFileList(),lawDTO.getId());
		return R.success("操作成功！");
	}

	@Override
	public R submit(LawDTO lawDTO){
		lawDTO.setState(Constant.FORM_STATE_VALID);
		lawDTO.setOpenTime(DateUtil.now());
		super.saveOrUpdate(lawDTO);
		//保存相关附件信息
		fileService.saveAttachments(lawDTO.getFileList(),lawDTO.getId());
		return R.success("操作成功！");
	}
}
