package com.starry.admin.modules.clerk.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.starry.admin.modules.clerk.mapper.PlayClerkWagesInfoMapper;
import com.starry.admin.modules.clerk.module.entity.PlayClerkUserInfoEntity;
import com.starry.admin.modules.clerk.module.entity.PlayClerkWagesInfoEntity;
import com.starry.admin.modules.clerk.module.vo.PlayClerkWagesInfoQueryVo;
import com.starry.admin.modules.clerk.module.vo.PlayClerkWagesInfoReturnVo;
import com.starry.admin.modules.clerk.service.IPlayClerkWagesInfoService;
import com.starry.admin.modules.personnel.service.IPlayPersonnelGroupInfoService;
import com.starry.admin.utils.SecurityUtils;
import com.starry.common.utils.IdUtils;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * 店员工资结算信息Service业务层处理
 *
 * @author admin
 * @since 2024-05-31
 */
@Service
public class PlayClerkWagesInfoServiceImpl extends ServiceImpl<PlayClerkWagesInfoMapper, PlayClerkWagesInfoEntity>
        implements
            IPlayClerkWagesInfoService {
    @Resource
    private PlayClerkWagesInfoMapper playClerkWagesInfoMapper;

    @Resource
    private IPlayPersonnelGroupInfoService playClerkGroupInfoService;

    @Override
    public PlayClerkWagesInfoEntity selectCurrentPeriodWagesByClerkId(String clerkId) {
        LambdaQueryWrapper<PlayClerkWagesInfoEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PlayClerkWagesInfoEntity::getClerkId, clerkId);
//        lambdaQueryWrapper.eq(PlayClerkWagesInfoEntity::getHistoricalStatistics, "0");
        return this.baseMapper.selectOne(lambdaQueryWrapper);
    }

    @Override
    public List<PlayClerkWagesInfoEntity> selectHistoricalWagesByClerkId(String clerkId) {
        LambdaQueryWrapper<PlayClerkWagesInfoEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PlayClerkWagesInfoEntity::getClerkId, clerkId);
//        lambdaQueryWrapper.eq(PlayClerkWagesInfoEntity::getHistoricalStatistics, "1");
        return this.baseMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public PlayClerkWagesInfoEntity getLastSettlement(String clerkId) {
        LambdaQueryWrapper<PlayClerkWagesInfoEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PlayClerkWagesInfoEntity::getClerkId, clerkId);
//        lambdaQueryWrapper.eq(PlayClerkWagesInfoEntity::getHistoricalStatistics, "0");
        lambdaQueryWrapper.orderByDesc(PlayClerkWagesInfoEntity::getEndCountDate);
        return this.baseMapper.selectOne(lambdaQueryWrapper);
    }

    @Override
    public LocalDate getLastSettlementTime(String clerkId) {
        LambdaQueryWrapper<PlayClerkWagesInfoEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PlayClerkWagesInfoEntity::getClerkId, clerkId);
//        lambdaQueryWrapper.eq(PlayClerkWagesInfoEntity::getHistoricalStatistics, "1");
        lambdaQueryWrapper.orderByDesc(PlayClerkWagesInfoEntity::getEndCountDate);
        PlayClerkWagesInfoEntity entity = this.baseMapper.selectOne(lambdaQueryWrapper);
        if (entity == null) {
            return LocalDate.of(2025, 1, 1);
        }
        return entity.getEndCountDate();
    }

    @Override
    public IPage<PlayClerkWagesInfoReturnVo> selectHistoricalByPage(PlayClerkWagesInfoQueryVo vo) {
        MPJLambdaWrapper<PlayClerkWagesInfoEntity> lambdaWrapper = new MPJLambdaWrapper<>();
        lambdaWrapper.selectAll(PlayClerkWagesInfoEntity.class);
        // 拼接店员表
        lambdaWrapper.selectAs(PlayClerkUserInfoEntity::getNickname, "clerkNickname");
        lambdaWrapper.leftJoin(PlayClerkUserInfoEntity.class, PlayClerkUserInfoEntity::getId,
                PlayClerkWagesInfoEntity::getClerkId);
        if (StrUtil.isNotBlank(vo.getGroupId())) {
            lambdaWrapper.eq(PlayClerkUserInfoEntity::getGroupId, vo.getGroupId());
        }
        if (StrUtil.isNotBlank(vo.getListingState())) {
            lambdaWrapper.eq(PlayClerkUserInfoEntity::getListingState, vo.getListingState());
        }
        if (StrUtil.isNotBlank(vo.getClerkId())) {
            lambdaWrapper.eq(PlayClerkUserInfoEntity::getId, vo.getClerkId());
        }
        if (StrUtil.isNotEmpty(vo.getSettlementDate())) {
            if (vo.getSettlementDate().contains("T")) {
                vo.setSettlementDate(vo.getSettlementDate().substring(0, vo.getSettlementDate().indexOf("T")));
            }
            lambdaWrapper.eq(PlayClerkWagesInfoEntity::getSettlementDate,
                    LocalDate.parse(vo.getSettlementDate(), DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        }
//        lambdaWrapper.eq(PlayClerkWagesInfoEntity::getHistoricalStatistics, "1");
        // 加入组员的筛选
        List<String> clerkIdList = playClerkGroupInfoService.getValidClerkIdList(SecurityUtils.getLoginUser(), null);
        lambdaWrapper.in(PlayClerkUserInfoEntity::getId, clerkIdList);
        return this.baseMapper.selectJoinPage(new Page<>(vo.getPageNum(), vo.getPageSize()),
                PlayClerkWagesInfoReturnVo.class, lambdaWrapper);
    }

    /**
     * 查询店员工资结算信息
     *
     * @param id
     *            店员工资结算信息主键
     * @return 店员工资结算信息
     */
    @Override
    public PlayClerkWagesInfoEntity selectPlayClerkWagesInfoById(String id) {
        return this.baseMapper.selectById(id);
    }

    /**
     * 查询店员工资结算信息列表
     *
     * @param playClerkWagesInfo
     *            店员工资结算信息
     * @return 店员工资结算信息
     */
    @Override
    public IPage<PlayClerkWagesInfoEntity> selectPlayClerkWagesInfoByPage(PlayClerkWagesInfoEntity playClerkWagesInfo) {
        Page<PlayClerkWagesInfoEntity> page = new Page<>(1, 10);
        return this.baseMapper.selectPage(page, new LambdaQueryWrapper<>());
    }

    /**
     * 新增店员工资结算信息
     *
     * @param playClerkWagesInfo
     *            店员工资结算信息
     * @return 结果
     */
    @Override
    public boolean create(PlayClerkWagesInfoEntity playClerkWagesInfo) {
        if (StrUtil.isBlankIfStr(playClerkWagesInfo.getId())) {
            playClerkWagesInfo.setId(IdUtils.getUuid());
        }
        return save(playClerkWagesInfo);
    }

    /**
     * 修改店员工资结算信息
     *
     * @param playClerkWagesInfo
     *            店员工资结算信息
     * @return 结果
     */
    @Override
    public boolean update(PlayClerkWagesInfoEntity playClerkWagesInfo) {
        return updateById(playClerkWagesInfo);
    }

    /**
     * 批量删除店员工资结算信息
     *
     * @param ids
     *            需要删除的店员工资结算信息主键
     * @return 结果
     */
    @Override
    public int deletePlayClerkWagesInfoByIds(String[] ids) {
        return playClerkWagesInfoMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除店员工资结算信息信息
     *
     * @param id
     *            店员工资结算信息主键
     * @return 结果
     */
    @Override
    public int deletePlayClerkWagesInfoById(String id) {
        return playClerkWagesInfoMapper.deleteById(id);
    }
}
