package com.zxdmy.excite.system.service.impl.cloud.account;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zxdmy.excite.common.entity.PageList;
import com.zxdmy.excite.common.exception.ServiceException;
import com.zxdmy.excite.system.entity.SysRoleMenu;
import com.zxdmy.excite.system.entity.SysUser;
import com.zxdmy.excite.system.entity.SysUserRole;
import com.zxdmy.excite.system.entity.cloud.account.CloudCompany;
import com.zxdmy.excite.system.entity.cloud.account.CloudDepartment;
import com.zxdmy.excite.system.entity.req.account.ReqCompany;
import com.zxdmy.excite.system.entity.vo.account.CompanyVo;
import com.zxdmy.excite.system.mapper.SysUserMapper;
import com.zxdmy.excite.system.mapper.cloud.account.CloudCompanyMapper;
import com.zxdmy.excite.system.service.cloud.account.ICloudCompanyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 企业表 服务实现类
 * </p>
 *
 * @author ss
 * @since 2026-01-24
 */
@Service
@AllArgsConstructor
public class CloudCompanyServiceImpl extends ServiceImpl<CloudCompanyMapper, CloudCompany> implements ICloudCompanyService {
    private CloudCompanyMapper cloudCompanyMapper;
    private SysUserMapper sysUserMapper;
    @Override
    public PageList<CompanyVo> queryByPage(Integer page, Integer limit, ReqCompany data) {
        Integer pages = limit * (page - 1);
        ArrayList<CompanyVo> list = cloudCompanyMapper.queryByPage(pages, limit,data);
        Integer count = cloudCompanyMapper.queryByPageCount(data);
        return new PageList<CompanyVo>(list,page,limit,count);
    }

    @Override
    public CompanyVo getByData(ReqCompany data) {
        return cloudCompanyMapper.getData(data);
    }

    @Override
    public int saveData(ReqCompany data) {
        LocalDateTime localDateTime = LocalDateTime.now();
        CloudCompany saveData = new CloudCompany();
        saveData.setCompanyName(data.getCompanyName());
        SysUser sysUser = sysUserMapper.selectById(StpUtil.getLoginIdAsInt());
        saveData.setCompanySort(0);
        saveData.setCreatorId(sysUser.getId());
        saveData.setCreatorName(sysUser.getUsername());
        saveData.setCreateTime(localDateTime);
        if(cloudCompanyMapper.insert(saveData)==1){
            return 1;
        }
        return 0;
    }

    @Override
    public int updateData(ReqCompany data) {
        CloudCompany updateData = new CloudCompany();
        updateData.setId(Long.valueOf(data.getId()));
        updateData.setCompanyName(data.getCompanyName());
        if(cloudCompanyMapper.updateById(updateData)==1){
            return 1;
        }
        return 0;
    }

    @Override
    public int deleteById(String id) {
        if (null == id) {
            throw new ServiceException("删除的ID不能为空！");
        }
        LocalDateTime localDateTime = LocalDateTime.now();
        SysUser sysUser = sysUserMapper.selectById(StpUtil.getLoginIdAsInt());
        CloudCompany data = cloudCompanyMapper.selectById(id);
        if(null!=data){
            data.setDeleteId(sysUser.getId());
            data.setDeleteName(sysUser.getUsername());
            data.setDeleteTime(localDateTime);
            return cloudCompanyMapper.updateById(data);
        }else{
            return 0;
        }
    }

    @Override
    public List<CompanyVo> queryByData() {
        return cloudCompanyMapper.queryByData();
    }

    @Override
    public List<CloudCompany> getList() {
        return cloudCompanyMapper.getList();
    }
}
