package com.muyu.server.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.muyu.common.core.utils.StringUtils;
import com.muyu.common.system.domain.SysUser;
import com.muyu.domain.Firm;
import com.muyu.domain.FirmPermission;
import com.muyu.domain.req.FirmListReq;
import com.muyu.domain.req.FirmUpdPermissionReq;
import com.muyu.domain.resp.FirmListResp;
import com.muyu.domain.resp.FirmTotalListResp;
import com.muyu.server.mapper.FirmManageMapper;
import com.muyu.server.service.FirmManageService;
import com.muyu.server.service.FirmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 公司+员工业务实现层
 * @Author：weiran
 * @Package：com.muyu.firmmanage.service.impl
 * @Project：cloud-server-8
 * @name：FirmManageServiceImpl
 * @Date：2024/9/27 12:28
 */
@Service
public class FirmManageServiceImpl extends ServiceImpl<FirmManageMapper, SysUser> implements FirmManageService {
    @Autowired
    private FirmManageMapper firmManageMapper;
    @Autowired
    private FirmService firmService;
    /**
     * 公司列表信息展示
     * @param firmListReq
     * @return
     */
    @Override
    public FirmTotalListResp firmmessageList(FirmListReq firmListReq) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        long count = this.count(queryWrapper);

        MPJLambdaWrapper<SysUser> wrapper = new MPJLambdaWrapper<>();
        wrapper.select(SysUser::getUserId,
                        SysUser::getDeptId,
                        SysUser::getUserName,
                        SysUser::getNickName,
                        SysUser::getEmail,
                        SysUser::getPhonenumber,
                        SysUser::getSex,
                        SysUser::getAvatar,
                        SysUser::getPassword,
                        SysUser::getStatus,
                        SysUser::getDelFlag,
                        SysUser::getLoginIp,
                        SysUser::getLoginDate,
                        SysUser::getDatabaseName,
                        SysUser::getFirmId)
                .selectAll(Firm.class)
                .selectAll(FirmPermission.class)
                .leftJoin(Firm.class,Firm::getFirmId,SysUser::getFirmId)
                .leftJoin(FirmPermission.class,FirmPermission::getPermissionId,Firm::getPermissionId)
                .eq(StringUtils.isNotEmpty(firmListReq.getFirmName()),
                        Firm::getFirmName, firmListReq.getFirmName());
        wrapper.last("LIMIT "+((firmListReq.getPageNum()-1)*firmListReq.getPageSize())+","+firmListReq.getPageSize());
        List<FirmListResp> firmListResps = firmManageMapper.selectJoinList(FirmListResp.class, wrapper);
        return FirmTotalListResp.firmTotalListResp(firmListResps,count);

    }


    /**
     * 修改企业对应权限
     * @param firmUpdPermissionReq
     * @param permissionId
     */
    @Override
    public void updpermission(FirmUpdPermissionReq firmUpdPermissionReq, long permissionId) {
        LambdaUpdateWrapper<Firm> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(Firm::getPermissionId, permissionId );
        updateWrapper.eq(Firm::getFirmId, firmUpdPermissionReq.getFirmId());
        firmService.update(updateWrapper);
    }
}
