package com.zcxk.charge.web.service;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zcxk.charge.core.dao.ThingyearSettingMapper;
import com.zcxk.charge.core.entity.ThingyearSettingEntity;
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 ThingyearSettingService extends ServiceImpl<ThingyearSettingMapper, ThingyearSettingEntity> {

    @Resource
    private ThingyearSettingMapper thingyearSettingMapper;
    @Autowired
    private OrgInfoUtil orgInfoUtil;

    /**
     * 查列表
     */
    public List<ThingyearSettingEntity> findList(ThingyearSettingEntity thingyearSettingEntity) {
        LoginUser currentUser = UserUtil.getCurrentUser();
        thingyearSettingEntity.setTenantId(currentUser.getTenantId());
        thingyearSettingEntity.setTenantId(currentUser.getTenantId());
        thingyearSettingEntity.setProgramItems(currentUser.getProgramItemList());
        thingyearSettingEntity.setUserType(currentUser.getType());
        //1是公司，2是公司及以下，3部门，4部门及以下，5自定义
        thingyearSettingEntity.setPermissonType(currentUser.getPermissonType());
        List<ThingyearSettingEntity> thingSettingEntities = thingyearSettingMapper.findList(thingyearSettingEntity);
        for (ThingyearSettingEntity 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 delThingyearSettingByIds(Long[] ids) {
        return this.removeByIds(Arrays.asList(ids));
    }

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

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

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

    /**
     * 根居ID获取对象
     */
    public ThingyearSettingEntity findThingyearSettingById(Long id) {
        return thingyearSettingMapper.findThingyearSettingById(id);
    }
}
