package com.staffing.custom.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.staffing.common.core.domain.AjaxResult;
import com.staffing.common.core.domain.entity.SysUser;
import com.staffing.common.utils.OrderUtils;
import com.staffing.common.utils.SecurityUtils;
import com.staffing.custom.domain.StEmployeeFundRecord;
import com.staffing.custom.domain.StTask;
import com.staffing.custom.mapper.StEmployeeFundRecordMapper;
import com.staffing.custom.service.IStEmployeeFundRecordService;
import com.staffing.custom.service.IStTaskService;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.staffing.custom.mapper.StTaskSignMapper;
import com.staffing.custom.domain.StTaskSign;
import com.staffing.custom.service.IStTaskSignService;

import static com.staffing.common.core.domain.AjaxResult.error;

/**
 * 签到Service业务层处理
 * 
 * @author staffing
 * @date 2024-08-27
 */
@Service
public class StTaskSignServiceImpl implements IStTaskSignService 
{
    @Autowired
    private StTaskSignMapper stTaskSignMapper;
    @Autowired
    private IStEmployeeFundRecordService employeeFundRecordService;
    @Autowired
    private IStTaskService stTaskService;
    @Autowired
    private StEmployeeFundRecordMapper stEmployeeFundRecordMapper;
    @Autowired
    private RedissonClient redissonClient;
    /**
     * 查询签到
     * 
     * @param id 签到主键
     * @return 签到
     */
    @Override
    public StTaskSign selectStTaskSignById(Long id)
    {
        return stTaskSignMapper.selectStTaskSignById(id);
    }

    /**
     * 查询签到列表
     * 
     * @param stTaskSign 签到
     * @return 签到
     */
    @Override
    public List<StTaskSign> selectStTaskSignList(StTaskSign stTaskSign)
    {
        return stTaskSignMapper.selectStTaskSignList(stTaskSign);
    }

    @Override
    public List<StTaskSign> selectStTaskSignByTaskIds(Long taskId)
    {
        return stTaskSignMapper.selectStTaskSignByTaskIds(Arrays.asList(taskId));
    }

    @Override
    public AjaxResult baoming(StTaskSign taskSign)
    {
        RLock lock = redissonClient.getLock(taskSign.getTaskId() + "");
        try{
            lock.lock();

            SysUser user = SecurityUtils.getLoginUser().getUser();
            long userId = user.getUserId();
            taskSign.setEmployeeId(userId);

            StTask stTask = stTaskService.selectStTaskById(taskSign.getTaskId());
            if (stTask.getUseNum() <= stTask.getListSign().size()){
                return error("报名已满");
            }
            for (StTaskSign stTaskSign : stTask.getListSign()) {
                if (stTaskSign.getEmployeeId().equals(taskSign.getEmployeeId())){
                    return error("无法重复报名");
                }
            }
            taskSign.setDayPrice(stTask.getDayPrice());
            taskSign.setEmployeeName(taskSign.getEmployeeName());
            taskSign.setStatus(-1);//未点名
            return AjaxResult.success(insertStTaskSign(taskSign));
        }finally {
            // 锁被持有
            if (lock != null && lock.isLocked()) {
                if (lock.isHeldByCurrentThread()) {
                    //  解锁
                    lock.unlock();
                }
            }
        }
    }

    /**
     * 新增签到
     * 
     * @param stTaskSign 签到
     * @return 结果
     */
    @Override
    public int insertStTaskSign(StTaskSign stTaskSign)
    {
        return stTaskSignMapper.insertStTaskSign(stTaskSign);
    }

    /**
     * 修改签到
     * 
     * @param stTaskSign 签到
     * @return 结果
     */
    @Override
    public int updateStTaskSign(StTaskSign stTaskSign)
    {
        return stTaskSignMapper.updateStTaskSign(stTaskSign);
    }


    /**
     * 签到
     * @return 结果
     */
    @Override
    public int signUp(StTaskSign updateStTaskSign)
    {
        StTaskSign stTaskSign = stTaskSignMapper.selectStTaskSignByTaskIdAndUserId(updateStTaskSign.getTaskId(), updateStTaskSign.getEmployeeId());
        if (stTaskSign == null){
            updateStTaskSign.setSignTime(new Date());
            return insertStTaskSign(updateStTaskSign);
        }
        if (stTaskSign.getSignTime() == null && (stTaskSign.getStatus() == 0 || stTaskSign.getStatus() == -1) && updateStTaskSign.getStatus() == 1){
            updateStTaskSign.setSignTime(new Date());
        }
        if (updateStTaskSign.getStatus() == 0){
            updateStTaskSign.setSignTime(null);
        }
        if (stTaskSign == null || updateStTaskSign.getStatus() != stTaskSign.getStatus()){
            if (updateStTaskSign.getStatus() == 1){
                StEmployeeFundRecord fundRecords = stEmployeeFundRecordMapper.selectStEmployeeFundRecordByUidAndTaskId(stTaskSign.getTaskId(), stTaskSign.getEmployeeId());
                if (fundRecords == null){
                    StTask stTask = stTaskService.selectStTaskById(stTaskSign.getTaskId());
                    StEmployeeFundRecord stEmployeeFundRecord = new StEmployeeFundRecord();
                    stEmployeeFundRecord.setTaskId(stTaskSign.getTaskId());
                    stEmployeeFundRecord.setTaskName(stTask.getTaskName());
                    stEmployeeFundRecord.setTaskDate(stTask.getTaskDate());
                    stEmployeeFundRecord.setEmployeeId(stTaskSign.getEmployeeId());
                    stEmployeeFundRecord.setEmployeeName(stTaskSign.getEmployeeName());
                    stEmployeeFundRecord.setPhone(stTaskSign.getPhone());
                    stEmployeeFundRecord.setIdCard(stTaskSign.getIdCard());
                    stEmployeeFundRecord.setDayPrice(stTaskSign.getDayPrice());
//            stEmployeeFundRecord.setShouldPrice(stTask.getDayPrice());
                    stEmployeeFundRecord.setRealPrice(stTask.getDayPrice());//默认实发为日结
//            stEmployeeFundRecord.setPayWay(0l);
//            stEmployeeFundRecord.setPayAccount(0l);
//            stEmployeeFundRecord.setPayTime();
                    stEmployeeFundRecord.setPayStatus(0l);
                    stEmployeeFundRecord.setUpdateTime(new Date());
                    stEmployeeFundRecord.setTransferNo(OrderUtils.genOrderId("D"));
                    employeeFundRecordService.insertStEmployeeFundRecord(stEmployeeFundRecord);
                }
            }else{
                stEmployeeFundRecordMapper.deleteStEmployeeFundRecordByUidAndTaskId(stTaskSign.getTaskId(), stTaskSign.getEmployeeId());
            }

        }

        return updateStTaskSignByTaskIdAndEmId(updateStTaskSign);
    }

    /**
     * 修改签到
     *
     * @param stTaskSign 签到
     * @return 结果
     */
    @Override
    public int updateStTaskSignByTaskIdAndEmId(StTaskSign stTaskSign)
    {
        return stTaskSignMapper.updateStTaskSignByTaskIdAndEmId(stTaskSign);
    }


    /**
     * 批量删除签到
     * 
     * @param ids 需要删除的签到主键
     * @return 结果
     */
    @Override
    public int deleteStTaskSignByIds(Long[] ids)
    {
        return stTaskSignMapper.deleteStTaskSignByIds(ids);
    }

    /**
     * 删除签到信息
     * 
     * @param id 签到主键
     * @return 结果
     */
    @Override
    public int deleteStTaskSignById(Long id)
    {
        return stTaskSignMapper.deleteStTaskSignById(id);
    }
}
