package com.xinda.iot.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.xinda.common.core.domain.AjaxResult;
import com.xinda.common.core.domain.entity.SysUser;
import com.xinda.common.utils.DateUtils;
import com.xinda.iot.domain.Standard;
import com.xinda.iot.domain.ThingsModelStandard;
import com.xinda.iot.mapper.StandardMapper;
import com.xinda.iot.mapper.ThingsModelStandardMapper;
import com.xinda.iot.model.IdAndName;
import com.xinda.iot.service.IStandardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.xinda.common.utils.SecurityUtils.getLoginUser;
import static com.xinda.common.utils.SecurityUtils.isAdmin;

/**
 * 产品分类Service业务层处理
 *
 * @author kerwincui
 * @date 2021-12-16
 */
@Service
public class StandardServiceImpl implements IStandardService
{
    @Autowired
    private StandardMapper standardMapper;

    @Autowired
    private ThingsModelStandardMapper thingsModelStandardMapper;
    /**
     * 查询产品分类
     *
     * @param standardId 产品分类主键
     * @return 产品分类
     */
    @Override
    public Standard selectStandardByStandardId(Long standardId)
    {
        return standardMapper.selectStandardByStandardId(standardId);
    }

    /**
     * 查询产品分类列表
     *
     * @param standard 产品分类
     * @return 产品分类
     */
    @Override
    public List<Standard> selectStandardList(Standard standard)
    {
        return standardMapper.selectStandardList(standard);
    }

    /**
     * 查询产品分简短类列表
     *
     * @return 产品分类
     */
    @Override
    public List<IdAndName> selectStandardShortList(Standard standard)
    {
        return standardMapper.selectStandardShortList(standard);
    }

    /**
     * 新增产品分类
     *
     * @param standard 产品分类
     * @return 结果
     */
    @Override
    public int insertStandard(Standard standard)
    {
        // 判断是否为管理员
        standard.setIsSys(1);
        SysUser user = getLoginUser().getUser();
//        List<SysRole> roles=user.getRoles();
//        for(int i=0;i<roles.size();i++){
//            if(roles.get(i).getRoleKey().equals("tenant") || roles.get(i).getRoleKey().equals("general")){
//                standard.setIsSys(0);
//                break;
//            }
//        }
        if (null != user.getDeptId()) {
            standard.setTenantId(user.getDept().getDeptUserId());
            standard.setTenantName(user.getDept().getDeptUserName());
        } else {
            standard.setTenantId(user.getUserId());
            standard.setTenantName(user.getUserName());
        }
        if (!isAdmin(user.getUserId())) {
            standard.setIsSys(0);
        }
        standard.setCreateTime(DateUtils.getNowDate());
        return standardMapper.insertStandard(standard);
    }
    @Override
    public int cloneStandard(Standard standard)
    {
        // 判断是否为管理员
        standard.setIsSys(1);
        SysUser user = getLoginUser().getUser();
        Standard cloneStandard = new Standard();
        BeanUtil.copyProperties(standard, cloneStandard, "standardId","standardName","orderNum");
        cloneStandard.setStandardName(standard.getStandardName()+"_副本");
        if (null != standard.getOrderNum()) {
            cloneStandard.setOrderNum(standard.getOrderNum()+1);
        }
//        cloneStandard.setOrderNum(standard.getOrderNum()+1);
//        if (null != user.getDeptId()) {
//            standard.setTenantId(user.getDept().getDeptUserId());
//            standard.setTenantName(user.getDept().getDeptUserName());
//        } else {
//            standard.setTenantId(user.getUserId());
//            standard.setTenantName(user.getUserName());
//        }
//        if (!isAdmin(user.getUserId())) {
//            standard.setIsSys(0);
//        }
        cloneStandard.setCreateTime(DateUtils.getNowDate());
        standardMapper.insertStandard(cloneStandard);
        List<ThingsModelStandard> thingsModelStandards = thingsModelStandardMapper.selectModelListByStandardId(standard.getStandardId());
        thingsModelStandardMapper.selectModelListByStandardId(standard.getStandardId()).forEach(model -> {
           model.setStandardId(cloneStandard.getStandardId());
            thingsModelStandards.add(model);
        });
        return thingsModelStandardMapper.batchThingsModelStandards(thingsModelStandards);
    }

    /**
     * 修改产品分类
     *
     * @param standard 产品分类
     * @return 结果
     */
    @Override
    public int updateStandard(Standard standard)
    {
        standard.setUpdateTime(DateUtils.getNowDate());
        return standardMapper.updateStandard(standard);
    }

    /**
     * 批量删除产品分类
     *
     * @param standardIds 需要删除的产品分类主键
     * @return 结果
     */
    @Override
    public AjaxResult deleteStandardByStandardIds(Long[] standardIds)
    {
        int productCount=standardMapper.productCountInStandards(standardIds);
        if(productCount>0){
            return AjaxResult.error("删除失败，请先删除对应分类下的产品");
        }
        if(standardMapper.deleteStandardByStandardIds(standardIds)>0){
            return AjaxResult.success("删除成功");
        }
        return AjaxResult.error("删除失败");
    }

    /**
     * 删除产品分类信息
     *
     * @param standardId 产品分类主键
     * @return 结果
     */
    @Override
    public int deleteStandardByStandardId(Long standardId)
    {
        return standardMapper.deleteStandardByStandardId(standardId);
    }
}
