package com.yeejoin.amos.boot.module.jcs.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yeejoin.amos.boot.module.jcs.api.dto.SinStaticDto;
import com.yeejoin.amos.boot.module.jcs.api.entity.Sign;
import com.yeejoin.amos.boot.module.jcs.api.mapper.SignMapper;
import com.yeejoin.amos.boot.module.jcs.api.service.ISignService;
import com.yeejoin.amos.boot.module.jcs.api.dto.SignDto;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.typroject.tyboot.core.foundation.utils.Bean;
import org.typroject.tyboot.core.rdbms.service.BaseService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.text.NumberFormat;
import java.util.List;

/**
 * 打卡记录服务实现类
 *
 * @author system_generator
 * @date 2022-08-15
 */
@Service
public class SignServiceImpl extends BaseService<SignDto,Sign,SignMapper> implements ISignService {
    /**
     * 分页查询
     */
    public Page<SignDto> queryForSignPage(Page<SignDto> page)   {
        return this.queryForPage(page, null, false);
    }

    /**
     * 分页查询
     */
    public IPage<SignDto> queryForSignPageByMapper(IPage<SignDto> page, SignDto dto)   {
        return this.baseMapper.queryForPage(page,dto);
    }

    /**
     * 分页查询
     */
    public IPage<SinStaticDto> queryForSignStaticPageByMapper(IPage<SinStaticDto> page, SinStaticDto dto)   {
        IPage<SinStaticDto> sinStaticDtoIPage = this.baseMapper.queryStaticForPage(page, dto);
        NumberFormat numberFormat = NumberFormat.getNumberInstance();
        numberFormat.setMaximumFractionDigits(2);

        sinStaticDtoIPage.getRecords().stream().forEach(e->{
            int num = this.baseMapper.queryPersonNum(e.getBizOrgCode());
            int signNum ;
            if((Integer.parseInt(e.getSignNum()) & 1) != 0) {
                signNum =  (Integer.parseInt(e.getSignNum()) + 1) / 2;
            } else {
                signNum = (Integer.parseInt(e.getSignNum())) / 2;
            }

            if(num != 0) {
                e.setAttendance(numberFormat.format((float) Integer.valueOf(signNum)/(float) num * 100) + "%");
            }  else {
                e.setAttendance("0%");
            }
        });

        return sinStaticDtoIPage;
    }

    /**
     * 列表查询 示例
     */
    public List<SignDto> queryForSignList()   {
        return this.queryForList("" , false);
    }

    @Override
    public Boolean hasSign(String type, String date,String userId) {
        QueryWrapper<Sign> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("date",date);
        if(StringUtils.isNotEmpty(type)) {
            queryWrapper.eq("type",type);
        }
        if(StringUtils.isNotEmpty(userId)) {
            queryWrapper.eq("user_id",userId);
        }
        List<Sign> signs = this.baseMapper.selectList(queryWrapper);

        if (StringUtils.isEmpty(type) && signs.size() != 2) {
            return true;
        } else {
            if(StringUtils.isNotEmpty(type) && signs.size() == 1) {
                return true;
            }
            return false;
        }
    }

    @Override
    public Boolean saveSign(SignDto dot) {
        Sign sign = new Sign();
        Bean.copyExistPropertis(dot,sign);
        return this.save(sign);
    }
}