package com.foreverwin.mesnac.dispatch.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.foreverwin.mesnac.common.enums.HandleEnum;
import com.foreverwin.mesnac.dispatch.dto.UserResourceDto;
import com.foreverwin.mesnac.dispatch.mapper.UserResourceMapper;
import com.foreverwin.mesnac.dispatch.model.UserResource;
import com.foreverwin.mesnac.dispatch.service.UserResourceService;
import com.foreverwin.mesnac.meapi.model.NwaUser;
import com.foreverwin.mesnac.meapi.service.NwaUserService;
import com.foreverwin.mesnac.meapi.service.ResrceService;
import com.foreverwin.modular.core.exception.BusinessException;
import com.foreverwin.modular.core.util.CommonMethods;
import com.foreverwin.modular.core.util.FrontPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author robert
 * @since 2021-06-24
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class UserResourceServiceImpl extends ServiceImpl<UserResourceMapper, UserResource> implements UserResourceService {


    @Autowired
    private UserResourceMapper userResourceMapper;


    @Autowired
    private ResrceService resrceService;

    @Autowired
    private NwaUserService nwaUserService;

    @Override
    public IPage<UserResource> selectPage(FrontPage<UserResource> frontPage, UserResource userResource) {
        QueryWrapper<UserResource> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(userResource);
        return super.page(frontPage.getPagePlus(), queryWrapper);
    }

    @Override
    public List<UserResource> selectList(UserResource userResource) {
        QueryWrapper<UserResource> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(userResource);
        return super.list(queryWrapper);
    }

    @Override
    public List<NwaUser> findEmployeeList(String site, String resource) {
        return userResourceMapper.findEmployeeList(site, resource);
    }

    @Override
    public List<UserResource> findEnableList(String site, String resource, String user) {
        return userResourceMapper.findEnableList(site, resource, user);
    }

    @Override
    public List<UserResourceDto> findAll(UserResourceDto userResourceDto) {
        String site = CommonMethods.getSite();
        String resource = userResourceDto.getResrce();
        String userId = userResourceDto.getUserId();
        return userResourceMapper.findAll(site, resource, userId);
    }

    @Override
    public void saveAll(List<UserResourceDto> userResourceDtoList) {
        List<UserResource> userResourceList = new ArrayList<>();
        String site = CommonMethods.getSite();
        for (int i = 0; i < userResourceDtoList.size(); i++) {
            UserResourceDto userResourceDto = userResourceDtoList.get(i);
            UserResource userResource = new UserResource();
            userResource.setSite(site);
            userResource.setResourceBo(HandleEnum.RESOURCE.getHandle(site, userResourceDto.getResrce()));
            userResource.setResrce(userResourceDto.getResrce());
            userResource.setResrceDescription(userResourceDto.getResrceDescription());
            if(resrceService.findByResrce(userResource.getResrce()) == null) {
                throw BusinessException.build("第"+(i+1)+"行的资源不存在");
            }
            userResource.setUserId(userResourceDto.getUserId());
            userResource.setTemporaryUser(userResourceDto.getTemporaryUser());
            userResource.setUserDescription(userResourceDto.getUserDescription());
            NwaUser userByUserName = nwaUserService.findUserByUserName(userResource.getUserId());
            if("false".equals(userResource.getTemporaryUser()) &&
                    userByUserName == null){
                throw BusinessException.build("第"+(i+1)+"行的用户不存在，必须为临时用户");
            }else if(userByUserName != null){
                userByUserName.setFullName(userResource.getUserDescription());
                nwaUserService.saveOrUpdate(userByUserName);
            }
            userResource.setStatus(userResourceDto.getStatus());
            userResource.setCreatedUser(CommonMethods.getUser());
            userResource.setHandle("UserResourceBo:"+userResource.getSite()+","+
                    userResource.getResrce()+","+userResource.getUserId());
            if(userResourceMapper.selectById(userResource.getHandle()) == null){
                userResource.setCreatedDateTime(LocalDateTime.now());
            }
            userResource.setModifiedUser(CommonMethods.getUser());
            userResource.setModifiedDateTime(LocalDateTime.now());

            userResourceList.add(userResource);
        }

        this.saveOrUpdateBatch(userResourceList);
    }



}