package cn.iocoder.yudao.module.smartfactory.service.staff;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.smartfactory.controller.admin.screendata.factory.vo.StaffDataRespVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.staff.vo.StaffPageReqVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.staff.vo.StaffSaveReqVO;
import cn.iocoder.yudao.module.smartfactory.dal.dataobject.staff.StaffDO;
import cn.iocoder.yudao.module.smartfactory.dal.mysql.staff.StaffMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

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


/**
 * 员工 Service 实现类
 *
 * @author 艾楷
 */
@Service
@Validated
public class StaffServiceImpl implements StaffService {

    @Resource
    private StaffMapper staffMapper;

    @Override
    public Long createStaff(StaffSaveReqVO createReqVO) {
        // 插入
        StaffDO staff = BeanUtils.toBean(createReqVO, StaffDO.class);
        staffMapper.insert(staff);
        // 返回
        return staff.getId();
    }

    @Override
    public void updateStaff(StaffSaveReqVO updateReqVO) {
        // 更新
        StaffDO updateObj = BeanUtils.toBean(updateReqVO, StaffDO.class);
        staffMapper.updateById(updateObj);
    }

    @Override
    public void deleteStaff(Long id) {
        // 删除
        staffMapper.deleteById(id);
    }

    @Override
    public StaffDO getStaff(Long id) {
        return staffMapper.selectById(id);
    }

    @Override
    public PageResult<StaffDO> getStaffPage(StaffPageReqVO pageReqVO) {
        return staffMapper.selectPage(pageReqVO);
    }

    @Override
    public StaffDataRespVO getStaffData(Long factoryId) {
        StaffDataRespVO vo = new StaffDataRespVO();
        List<StaffDO> dos = staffMapper.getStaffData(factoryId);
        vo.setTotal(dos.size());
        vo.setMaleTotal((int) dos.stream().filter(a -> a.getSex() == 0).count());
        vo.setFemaleTotal((int) dos.stream().filter(a -> a.getSex() == 1).count());
        vo.setStaffInfos(dos);
        return vo;
    }

    @Override
    public Integer getCount() {
        return Math.toIntExact(staffMapper.selectCount(new LambdaQueryWrapperX<StaffDO>().eq(StaffDO::getStatus, 1)));
    }

}