package com.xunmei.core.resumption.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 com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xunmei.common.core.vo.IdNameVo;
import com.xunmei.common.core.web.page.TableDataInfo;
import com.xunmei.common.security.utils.SecurityUtils;
import com.xunmei.core.resumption.domain.AppRule;
import com.xunmei.core.resumption.dto.appRule.AppRuleListForOrgDto;
import com.xunmei.core.resumption.mapper.AppRuleMapper;
import com.xunmei.core.resumption.dto.appRule.AppRuleEditDto;
import com.xunmei.core.resumption.service.IAppRuleService;
import com.xunmei.core.resumption.vo.appRule.AppRuleInfoVo;
import com.xunmei.core.resumption.vo.appRule.AppRulePageVo;
import com.xunmei.system.api.RemoteOrgService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.xunmei.common.core.utils.IDHelper;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author system
 * @since 2022-04-26
 */
@Slf4j
@Service("appRuleService")
public class AppRuleServiceImpl extends ServiceImpl<AppRuleMapper, AppRule> implements IAppRuleService {
    @Resource
    private AppRuleMapper appRuleMapper;

    @Resource
    RemoteOrgService remoteOrgService;

    @Override
    public TableDataInfo getAppRulePage(AppRule appRule) {
        Page<AppRulePageVo> page = appRule.<AppRulePageVo>toPage();
        page = appRuleMapper.selectPageByCondition(page, appRule);

        return TableDataInfo.success(page);
    }

    @Override
    public List<AppRulePageVo> getAllAppRule() {
        List<AppRule> all = appRuleMapper.selectList(new QueryWrapper());
        List<AppRulePageVo> r = new ArrayList<>();
        for (AppRule appRule : all) {
            AppRulePageVo vo = new AppRulePageVo();
            BeanUtils.copyProperties(appRule, vo);
            r.add(vo);
        }

        return r;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long edit(AppRuleEditDto appRuleEditDto) {
        final AppRule appRule = new AppRule();
        BeanUtils.copyProperties(appRuleEditDto, appRule);
        Integer nums = appRuleMapper.checkRuleNameRepeat(appRule.getId(), appRule.getName());
        if (nums > 0) {
            throw new RuntimeException("规则名称重复");
        }

        if (appRule.getId() == null) {
            appRule.setId(IDHelper.id());
//            appRule.setOrgId(4352539158904832L);
            appRule.setOrgId(SecurityUtils.getLoginUser().getSysUser().getOrgId());
            save(appRule);
        } else {
            updateById(appRule);
//            appRuleMapper.updateItemRuleType(appRuleEditDto.getId(), appRuleEditDto.getRuleTypeId());
//            appRuleMapper.updatePointRuleType(appRuleEditDto.getId(), appRuleEditDto.getRuleTypeId());
        }
        return appRule.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void del(Long id) {
        appRuleMapper.deleteById(id);
    }

    @Override
    public AppRuleInfoVo get(Long id) {
        AppRule rule = appRuleMapper.selectById(id);
        return AppRuleInfoVo.of(rule);
    }

    @Override
    public List<IdNameVo> ruleListForOrg(AppRuleListForOrgDto dto) {
        Long loginOrgId = SecurityUtils.getLoginUser().getOrgId();
        List<Long> upOrgIds = remoteOrgService.getUpOrgs(loginOrgId);

        List<AppRule> rules = appRuleMapper.selectList(new LambdaQueryWrapper<AppRule>()
                .in(AppRule::getOrgId, upOrgIds)
                .eq(AppRule::getOrgType, dto.getOrgType())
                .eq(AppRule::getStatus, 0)
                .select(AppRule::getId, AppRule::getName)
        );
        List<IdNameVo> list = rules.stream().map(r -> {
            IdNameVo vo = new IdNameVo();
            BeanUtils.copyProperties(r, vo);
            return vo;
        }).collect(Collectors.toList());

        return list;
    }
}
