package com.zcxk.charge.web.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zcxk.charge.core.dao.ThingSettingMapper;
import com.zcxk.charge.core.entity.ThingSettingEntity;
import com.zcxk.charge.web.util.OrgInfoUtil;
import com.zcxk.core.oauth2.pojo.LoginUser;
import com.zcxk.core.oauth2.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 物品设置Service接口
 *
 * @author: WYY
 * @date 2020-12-28 15:18
 */
@Service
public class ThingSettingService extends ServiceImpl<ThingSettingMapper, ThingSettingEntity> {

    @Resource
    private ThingSettingMapper thingSettingMapper;
    @Autowired
    private OrgInfoUtil orgInfoUtil;


    /**
     * 查列表
     */
    public List<ThingSettingEntity> findList(ThingSettingEntity thingSettingEntity) {
        LoginUser currentUser = UserUtil.getCurrentUser();
        thingSettingEntity.setTenantId(currentUser.getTenantId());
        thingSettingEntity.setProgramItems(currentUser.getProgramItemList());
        thingSettingEntity.setUserType(currentUser.getType());
        //1是公司，2是公司及以下，3部门，4部门及以下，5自定义
        thingSettingEntity.setPermissonType(currentUser.getPermissonType());
        List<ThingSettingEntity> thingSettingEntities = thingSettingMapper.findList(thingSettingEntity);

        for (ThingSettingEntity item : thingSettingEntities) {
            if (item.getCompanyOrgId() != null) {
                item.setCompanyOrgName(orgInfoUtil.getOrgName(item.getCompanyOrgId().intValue()));
            }
            if (item.getDeptOrgId() != null) {
                item.setDeptOrgName(orgInfoUtil.getOrgName(item.getDeptOrgId().intValue()));
            }
        }
        return thingSettingEntities;
    }

    /**
     * 批量删除
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean delThingSettingByIds(Long[] ids) {
        return this.removeByIds(Arrays.asList(ids));
    }

    /**
     * 单个删除
     */
    public boolean delThingSettingById(Long id) {
        return this.removeById(id);
    }

    /**
     * 保存
     */
    public boolean addThingSetting(ThingSettingEntity thingSetting) {
        LoginUser currentUser = UserUtil.getCurrentUser();
        thingSetting.setTenantId(currentUser.getTenantId());
        thingSetting.setStatus(1);
        if (currentUser != null) {
            thingSetting.setCreateBy(currentUser.getName());
            thingSetting.setUpdateBy(currentUser.getName());
            thingSetting.setDateUpdate(new Date());
            thingSetting.setDateCreate(new Date());
        }
        if (thingSetting.getDeptOrgId() != null && thingSetting.getDeptOrgId() == 0) {
            thingSetting.setDeptOrgId(null);
        }
        if (this.save(thingSetting)) {
            return true;
        }
        return false;
    }

    /**
     * 修改根居ID
     */
    public boolean updateThingSettingById(ThingSettingEntity thingSetting) {
        LoginUser currentUser = UserUtil.getCurrentUser();
        thingSetting.setUpdateBy(currentUser.getName());
        if (thingSetting.getDeptOrgId() != null && thingSetting.getDeptOrgId() == 0) {
            thingSetting.setDeptOrgId(null);
        }
        if (this.updateById(thingSetting)) {
            return true;
        }
        return false;
    }

    /**
     * 根居ID获取对象
     */
    public ThingSettingEntity findThingSettingById(Long id) {
        return thingSettingMapper.findThingSettingById(id);
    }

    public List<ThingSettingEntity> findTotalList(ThingSettingEntity thingSettingEntity) {
        LoginUser currentUser = UserUtil.getCurrentUser();
        thingSettingEntity.setTenantId(currentUser.getTenantId());
        thingSettingEntity.setProgramItems(currentUser.getProgramItemList());
        thingSettingEntity.setUserType(currentUser.getType());
        //1是公司，2是公司及以下，3部门，4部门及以下，5自定义
        thingSettingEntity.setPermissonType(currentUser.getPermissonType());
        List<ThingSettingEntity> thingSettingEntities = thingSettingMapper.findTotalList(thingSettingEntity);

        return thingSettingEntities;
    }
}
