/*
 *    Copyright (c) 2018-2025, hnqz 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 pig4cloud.com 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: hnqz
 */
package com.qunzhixinxi.hnqz.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qunzhixinxi.hnqz.admin.api.entity.SysDept;
import com.qunzhixinxi.hnqz.admin.api.entity.SysDeptRelation;
import com.qunzhixinxi.hnqz.admin.entity.SysDeptSub;
import com.qunzhixinxi.hnqz.admin.entity.WmDaDrugEnt;
import com.qunzhixinxi.hnqz.admin.enums.SubjectTypeEnum;
import com.qunzhixinxi.hnqz.admin.mapper.SysDeptSubMapper;
import com.qunzhixinxi.hnqz.admin.mapper.WmDaDrugEntMapper;
import com.qunzhixinxi.hnqz.admin.service.SysDeptRelationService;
import com.qunzhixinxi.hnqz.admin.service.SysDeptService;
import com.qunzhixinxi.hnqz.admin.service.WmDaDrugEntService;
import com.qunzhixinxi.hnqz.common.data.datascope.DataScope;
import com.qunzhixinxi.hnqz.common.security.util.SecurityUtils;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 档案-药企
 *
 * @author gaoyanng
 * @date 2020-06-25 23:35:42
 */
@Service
@AllArgsConstructor
public class WmDaDrugEntServiceImpl extends ServiceImpl<WmDaDrugEntMapper, WmDaDrugEnt> implements WmDaDrugEntService {

	private final SysDeptService sysDeptService;
	private final WmDaDrugEntMapper wmDaDrugEntMapper;
	private final SysDeptRelationService sysDeptRelationService;
	private final SysDeptSubMapper sysDeptSubMapper;

	@Override
	@Transactional
	public boolean saveDrugEnt(WmDaDrugEnt entity) {
		Integer deptId = Integer.parseInt(System.currentTimeMillis() / 1000 + "");
		entity.setDeptId(deptId + "");
		entity.setCreateTime(LocalDateTime.now());
		boolean isSave = super.save(entity);
		if (isSave) {
			SysDept sysDept = new SysDept();
			sysDept.setDeptId(deptId);
			sysDept.setName(entity.getEntname());
			sysDept.setLevel(2);
			sysDept.setLevel1Id(Integer.parseInt(entity.getId()));
			sysDept.setLevel1Name(entity.getEntname());
			sysDept.setSubjectType(entity.getSubjectType());
			sysDept.setTaxCode(entity.getEntcode());
			/**
			 * @Modify: start
			 * @Author: ryz
			 * @Date: 2021/4/19
			 */
			if(sysDeptService.saveDept(sysDept) > 0){
				for (String subType : entity.getSubjectType()) {
					SysDeptSub deptSub = new SysDeptSub();
					deptSub.setDeptId(sysDept.getDeptId());
					deptSub.setSubjectType(subType);
					deptSub.setEnableFlag(SubjectTypeEnum.ENABLE_FLAG_TRUE.getCode());
					sysDeptSubMapper.insert(deptSub);
				}
				return true;
			}
			return  false;

			/**
			 * @Modify: end
			 */

//			return sysDeptService.saveDept(sysDept) > 0 ? true : false;
		}
		return false;
	}

	@Override
	public List<WmDaDrugEnt> selectByScope(Wrapper<WmDaDrugEnt> queryWrapper) {
		return wmDaDrugEntMapper.selectListByScope(queryWrapper, new DataScope());

	}

	@Override
	public List<WmDaDrugEnt> selectByDrugId(WmDaDrugEnt wmDaDrugEnt) {
		return wmDaDrugEntMapper.selectByDrugId(wmDaDrugEnt);
	}

	@Override
	public List selectEntNameByDrugId(Integer id) {
		return wmDaDrugEntMapper.selectEntNameByDrugId(id);
	}

	@Override
	public List selectEntNameByDrugIds(Integer id) {
		return wmDaDrugEntMapper.selectEntNameByDrugIds(id);
	}

	@Override
	public boolean saveDrugEnts(WmDaDrugEnt entity) {
		Integer deptId = Integer.parseInt(System.currentTimeMillis() / 1000 + "");
		entity.setDeptId(deptId + "");
		entity.setCreateTime(LocalDateTime.now());
		boolean isSave = super.save(entity);
		if (isSave) {
			SysDept sysDept = new SysDept();
//			sysDept.setDeptId(deptId);
			sysDept.setName(entity.getEntname());
			sysDept.setLevel(2);
//			return sysDeptService.saveDept(sysDept)>0?true:false;
			sysDept.setSubjectType(entity.getSubjectType());
			sysDept.setDeptPermissions(entity.getDeptPermissions());
			sysDeptService.save(sysDept);
			entity.setDeptId(String.valueOf(sysDept.getDeptId()));
			wmDaDrugEntMapper.updateById(entity);
			List<SysDeptRelation> newList = new ArrayList<>();
			SysDeptRelation sysDeptRelation = new SysDeptRelation();
			sysDeptRelation.setAncestor(SecurityUtils.getUser().getDeptId());
			sysDeptRelation.setDescendant(Integer.valueOf(entity.getDeptId()));
			newList.add(sysDeptRelation);
			SysDeptRelation sysDeptRelation1 = new SysDeptRelation();
			sysDeptRelation1.setAncestor(Integer.valueOf(entity.getDeptId()));
			sysDeptRelation1.setDescendant(Integer.valueOf(entity.getDeptId()));
			newList.add(sysDeptRelation1);
			sysDeptRelationService.saveBatch(newList);
			/**
			 * @Modify: start
			 * @Author: ryz
			 * @Date: 2021/4/19
			 */
			for (String subType : entity.getSubjectType()) {
				SysDeptSub deptSub = new SysDeptSub();
				deptSub.setDeptId(sysDept.getDeptId());
				deptSub.setSubjectType(subType);
				deptSub.setEnableFlag(SubjectTypeEnum.ENABLE_FLAG_TRUE.getCode());
				sysDeptSubMapper.insert(deptSub);
			}
			return true;
		}
		return false;
	}

	@Override
	public WmDaDrugEnt selectByDeptId(Integer deptId) {
		return wmDaDrugEntMapper.selectByDeptId(deptId);
	}

	@Override
	public List selectEntNameByDeptId(Integer deptId) {
		return wmDaDrugEntMapper.selectEntNameByDeptId(deptId);
	}

	@Override
	public List selectEntNameByByDeptIds(Integer upId) {
		return wmDaDrugEntMapper.selectEntNameByByDeptIds(upId);
	}

	@Override
	public List<WmDaDrugEnt> getListByCsoDeptId(Integer deptId) {
		return wmDaDrugEntMapper.getListByCsoDeptId(deptId);
	}

	/**
	 * 分页查询
	 *
	 * @param page 分页对象
	 * @param wmDaDrugEnt 实体对象
	 * @return 分页列表
	 */
	@Override
	public IPage<List<WmDaDrugEnt>> selectPage(Page page, WmDaDrugEnt wmDaDrugEnt) {
		return wmDaDrugEntMapper.selectPage(page, wmDaDrugEnt);
	}
}
