package com.dgtly.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.dgtly.common.constant.UserConstants;
import com.dgtly.common.core.domain.Ztree;
import com.dgtly.common.utils.DateUtils;
import com.dgtly.system.service.ISysConfigService;
import com.dgtly.system.service.ISysDeptService;
import com.dgtly.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dgtly.system.mapper.SysCompanyMapper;
import com.dgtly.system.domain.SysCompany;
import com.dgtly.system.service.ISysCompanyService;
import com.dgtly.common.core.text.Convert;

/**
 * 公司表Service业务层处理
 * 
 * @author dgtly
 * @date 2020-01-08
 */
@Service
public class SysCompanyServiceImpl implements ISysCompanyService 
{
    @Autowired
    private SysCompanyMapper sysCompanyMapper;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysDeptService sysDeptService;
    @Autowired
    private ISysConfigService sysConfigService;



        /**
         * 查询公司表
         *
         * @param id 公司表ID
         * @return 公司表
         */
    @Override
    public SysCompany selectSysCompanyById(Long id)
    {
        return sysCompanyMapper.selectSysCompanyById(id);
    }

    /**
     * 查询公司表列表
     * 
     * @param sysCompany 公司表
     * @return 公司表
     */
    @Override
    public List<SysCompany> selectSysCompanyList(SysCompany sysCompany)
    {
        return sysCompanyMapper.selectSysCompanyList(sysCompany);
    }

    /**
     * 新增公司表
     * 
     * @param sysCompany 公司表
     * @return 结果
     */
    @Override
    public int insertSysCompany(SysCompany sysCompany)
    {
        sysCompany.setCreateTime(DateUtils.getNowDate());
        return sysCompanyMapper.insertSysCompany(sysCompany);
    }

    /**
     * 修改公司表
     *
     * @param sysCompany 公司表
     * @return 结果
     */
    @Override
    public int updateSysCompany(SysCompany sysCompany)
    {
        sysCompany.setUpdateTime(DateUtils.getNowDate());
        return sysCompanyMapper.updateSysCompany(sysCompany);
    }

    /**
     * 删除公司表对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteSysCompanyByIds(String ids)
    {
        return sysCompanyMapper.deleteSysCompanyByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除公司表信息
     * 
     * @param id 公司表ID
     * @return 结果
     */
    @Override
    public int deleteSysCompanyById(Long id)
    {
        return sysCompanyMapper.deleteSysCompanyById(id);
    }


    @Override
    public List<Ztree> selectSysCompanyTree(SysCompany sysCompany) {
        List<SysCompany> companies=sysCompanyMapper.selectSysCompanyList(sysCompany);
        return initZtree(companies);
    }

    /**
     * 查询热门商家列表
     * @param
     * @return
     */
    @Override
    public List<SysCompany> selectCompanyHotList() {
        return sysCompanyMapper.selectCompanyHotList();
    }

    /**
     * 查询公司表排除根目录公司
     * @param:
     * @return:
     * @auther: qxp
     * @date: 2020-2-25
     */
    @Override
    public List<SysCompany> selectSysCompanyListExRoot(SysCompany sysCompany) {
        return sysCompanyMapper.selectSysCompanyListExRoot(sysCompany);
    }



    /**
     * @descption: 树形结构初始化
     * @param:
     * @return:
     * @auther: qxp
     * @date: 2020-2-25
     */
    public List<Ztree> initZtree(List<SysCompany> companies) {
        List<Ztree> ztrees = new ArrayList<Ztree>();
        for (SysCompany company : companies)
        {
            if (UserConstants.DEPT_NORMAL.equals(company.getStatus().toString())&&company.getId()!=1L)
            {
                Ztree ztree = new Ztree();
                ztree.setId(company.getId());
                ztree.setpId(0L);
                ztree.setName(company.getCompanyName());
                ztree.setTitle(company.getCompanyName());
                ztrees.add(ztree);
            }
        }
        return ztrees;
    }


    @Override
    public int countCompany() {
        return sysCompanyMapper.countCompany();
    }
}
