
package com.dxhy.admin.sys.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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.dxhy.admin.sys.dao.SysBusinessDao;
import com.dxhy.admin.sys.entity.SysBusinessEntity;
import com.dxhy.admin.sys.service.SysBusinessService;
import com.dxhy.admin.sys.service.SysUserService;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.utils.Aops;
import com.dxhy.common.utils.Constant;
import com.dxhy.common.utils.PageUtils;
import com.dxhy.common.utils.Query;

import lombok.extern.slf4j.Slf4j;

/**
 * 业务单元
 *
 * @author jiaohongyang
 */
@Service("sysBusinessService")
@Slf4j
public class SysBusinessServiceImpl extends ServiceImpl<SysBusinessDao, SysBusinessEntity>
    implements SysBusinessService {
    @Resource
    private SysUserService sysUserService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        String businessName = (String)params.get("businessName");
        String deptId = (String)params.get("deptId");
        String company = (String)params.get("company");
        IPage<SysBusinessEntity> page = this.page(new Query<SysBusinessEntity>().getPage(params),
            new QueryWrapper<SysBusinessEntity>()
                .like(StringUtils.isNotBlank(businessName), "business_name", businessName)
                .eq(StringUtils.isNotBlank(company), "company", company)
                .eq(StringUtils.isNotBlank(deptId), "dept_id", deptId)
                .apply(params.get(Constant.SQL_FILTER) != null, (String)params.get(Constant.SQL_FILTER)));

        return new PageUtils(page);
    }

    @Override
    public void saveBusiness(SysBusinessEntity businessEntity) {
        DynamicContextHolder.push(DbConstant.BASICS_WRITE);
        businessEntity.setCreateTime(new Date());
        Aops.getSelf(this).insertBusiness(businessEntity);
    }

    @Transactional(rollbackFor = Exception.class)
    public void insertBusiness(SysBusinessEntity businessEntity) {
        baseMapper.insert(businessEntity);
    }

    @Override
    public void update(SysBusinessEntity businessEntity) {
        DynamicContextHolder.push(DbConstant.BASICS_WRITE);
        Aops.getSelf(this).updateBusiness(businessEntity);
        // 刷新关联用户缓存
        this.renovateBusiness(businessEntity);
    }

    public void renovateBusiness(SysBusinessEntity businessEntity) {
        sysUserService.queryUserBussiness(businessEntity.getId());
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateBusiness(SysBusinessEntity businessEntity) {
        baseMapper.updateById(businessEntity);
    }

    @Override
    public void deleteBatch(Long businessIds) {
        DynamicContextHolder.push(DbConstant.BASICS_WRITE);
        // 删除业务单元
        Aops.getSelf(this).removeBusiness(businessIds);
    }

    @Transactional(rollbackFor = Exception.class)
    public void removeBusiness(Long businessIds) {
        baseMapper.deleteById(businessIds);
    }

    @Override
    public List<SysBusinessEntity> queryBusinessDeptId(String company) {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        return baseMapper.selectList(new QueryWrapper<SysBusinessEntity>().eq("company", company));
    }

    @Override
    public List<SysBusinessEntity> queryBusinesslist() {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        return baseMapper.selectList(null);
    }

    @Override
    public SysBusinessEntity queryBusinessById(Long id) {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        return baseMapper.selectById(id);
    }

    @Override
    public SysBusinessEntity queryBusiness(String businessCode, String company, Long businessId) {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        return baseMapper.queryBusinessCode(businessCode, businessId, company);
    }
}
