package cn.iocoder.yudao.module.system.api.holiday;

import cn.hutool.core.bean.BeanUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.system.api.holiday.dto.CreateUserHolidayDTO;
import cn.iocoder.yudao.module.system.api.holiday.dto.OvertimeIncreasesHolidayDTO;
import cn.iocoder.yudao.module.system.api.holiday.vo.HolidaySettingVO;
import cn.iocoder.yudao.module.system.dal.dataobject.holiday.holidaysetting.HolidaySettingDO;
import cn.iocoder.yudao.module.system.dal.dataobject.holiday.holidayuser.HolidayUserDO;
import cn.iocoder.yudao.module.system.dal.mysql.holiday.holidayuser.HolidayUserMapper;
import cn.iocoder.yudao.module.system.service.holiday.holidaysetting.HolidaySettingService;
import cn.iocoder.yudao.module.system.service.holiday.holidayuserrecord.HolidayUserRecordService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController // 提供 RESTful API 接口，给 Feign 调用
@Validated
public class HolidayApiImpl implements HolidayApi {

    @Resource
    private HolidaySettingService holidaySettingService;
    @Resource
    private HolidayUserMapper holidayUserMapper;
    @Resource
    private HolidayUserRecordService holidayUserRecordService;


    @Override
    public CommonResult<HolidaySettingVO> getHolidaySettings(Long userId, Long holidaySettingId) {
        HolidaySettingVO vo = new HolidaySettingVO();
        HolidaySettingDO holidaySetting = holidaySettingService.getHolidaySetting(holidaySettingId);
        HolidayUserDO holidayUserDO = holidayUserMapper.selectOne(new LambdaQueryWrapper<HolidayUserDO>().eq(HolidayUserDO::getHolidaySettingId, holidaySettingId).eq(HolidayUserDO::getUserId, userId));
        BeanUtil.copyProperties(holidaySetting, vo);
        vo.setHolidayBalance(holidayUserDO == null ? null : holidayUserDO.getHolidayBalance());
        vo.setStatus(holidaySetting.getHolidayBalanceSettingDO().getStatus());
        return CommonResult.success(vo);
    }

    @Override
    public CommonResult createUserHoliday(CreateUserHolidayDTO dto) {
        holidayUserRecordService.createUserHoliday(dto);
        return CommonResult.success("ok");
    }

    @Override
    public CommonResult overtimeIncreasesHoliday(OvertimeIncreasesHolidayDTO dto) {
        holidayUserRecordService.overtimeIncreasesHoliday(dto);
        return CommonResult.success("ok");
    }

}
