package com.prolog.rdc.tms.service.impl;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;
import com.prolog.cs.platform.power.model.Organization;
import com.prolog.framework.core.restriction.Criteria;
import com.prolog.framework.core.restriction.Restriction;
import com.prolog.framework.core.restriction.Restrictions;
import com.prolog.product.core.framework.entity.UserEntity;
import com.prolog.product.core.framework.util.PlgFxUsrUtils;
import com.prolog.rdc.tms.config.enums.DepartmentBaseLinkBaseTypeEnum;
import com.prolog.rdc.tms.constant.SeriaNnumberConstant;
import com.prolog.rdc.tms.dao.mapper.TmsDepartmentBaseLinkMapper;
import com.prolog.rdc.tms.model.entity.line.TmsDepartmentBaseLinkModel;
import com.prolog.rdc.tms.service.DepartmentBaseLinkService;
import com.prolog.rdc.tms.service.SysCodeengineService;

/**
 * @author hongbo.pan
 * @date 2022/10/9
 */
@Service
public class DepartmentBaseLinkServiceImpl implements DepartmentBaseLinkService {

	@Autowired
	private SysCodeengineService sysCodeengineService;

	@Autowired
	private TmsDepartmentBaseLinkMapper tmsDepartmentBaseLinkMapper;

	@Transactional(rollbackFor = Exception.class)
	@Override
	public void addLink(DepartmentBaseLinkBaseTypeEnum typeEnum, String baseId, String departmentId, String departmentCode, String departmentName) {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		List<String> departmentIdList = Lists.newArrayList(departmentId.split(","));
		List<String> departmentCodeList = Lists.newArrayList(departmentCode.split(","));
		List<String> departmentNameList = Lists.newArrayList(departmentName.split(","));
		List<TmsDepartmentBaseLinkModel> linkList = Lists.newArrayList();
		for (int i = 0; i < departmentIdList.size(); i++) {
			String linkId = sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.TMS_DEPARTMENT_BASE_LINK_ID, "L");
			TmsDepartmentBaseLinkModel tmsDepartmentBaseLinkModel = new TmsDepartmentBaseLinkModel();
			tmsDepartmentBaseLinkModel.setId(Long.valueOf(linkId));
			tmsDepartmentBaseLinkModel.setBaseType(typeEnum.getCode());
			tmsDepartmentBaseLinkModel.setBaseId(baseId);
			tmsDepartmentBaseLinkModel.setDepartmentId(departmentIdList.get(i));
			tmsDepartmentBaseLinkModel.setDepartmentCode(departmentCodeList.get(i));
			tmsDepartmentBaseLinkModel.setDepartmentName(departmentNameList.get(i));
			tmsDepartmentBaseLinkModel.setCreateperson(user.getNickname());
			tmsDepartmentBaseLinkModel.setCreatetime(new Date());
			tmsDepartmentBaseLinkModel.setUpdateperson(user.getNickname());
			tmsDepartmentBaseLinkModel.setUpdatetime(new Date());
			linkList.add(tmsDepartmentBaseLinkModel);
		}
		tmsDepartmentBaseLinkMapper.saveBatch(linkList);
	}

	@Override
	public void addLink(DepartmentBaseLinkBaseTypeEnum typeEnum, String baseId, List<Organization> departmentList) {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		List<TmsDepartmentBaseLinkModel> linkList = departmentList.stream().map(department -> {
			String linkId = sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.TMS_DEPARTMENT_BASE_LINK_ID, "L");
			TmsDepartmentBaseLinkModel tmsDepartmentBaseLinkModel = new TmsDepartmentBaseLinkModel();
			tmsDepartmentBaseLinkModel.setId(Long.valueOf(linkId));
			tmsDepartmentBaseLinkModel.setBaseType(typeEnum.getCode());
			tmsDepartmentBaseLinkModel.setBaseId(baseId);
			tmsDepartmentBaseLinkModel.setDepartmentId(department.getId());
			tmsDepartmentBaseLinkModel.setDepartmentCode(department.getCode());
			tmsDepartmentBaseLinkModel.setDepartmentName(department.getName());
			tmsDepartmentBaseLinkModel.setCreateperson(user.getNickname());
			tmsDepartmentBaseLinkModel.setCreatetime(new Date());
			tmsDepartmentBaseLinkModel.setUpdateperson(user.getNickname());
			tmsDepartmentBaseLinkModel.setUpdatetime(new Date());
			return tmsDepartmentBaseLinkModel;
		}).collect(Collectors.toList());
		tmsDepartmentBaseLinkMapper.saveBatch(linkList);
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public void deleteLink(DepartmentBaseLinkBaseTypeEnum typeEnum, List<String> baseIdList) {
		Criteria criteria = Criteria.forClass(TmsDepartmentBaseLinkModel.class);
		Restriction r1 = Restrictions.eq("baseType", typeEnum.getCode());
		Restriction r2 = Restrictions.in("baseId", baseIdList.toArray());
		criteria.setRestriction(Restrictions.and(r1, r2));
		tmsDepartmentBaseLinkMapper.deleteByCriteria(criteria);
	}
}
