package com.ruoyi.pay.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;

import com.ruoyi.pay.domain.PayDayWorkHoursQuery;
import com.ruoyi.pay.domain.PayDwHoursContrast;
import com.ruoyi.pay.domain.vo.PayDayWorkHoursExport;
import com.ruoyi.pay.domain.vo.PayDayWorkHoursQuery2;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.pay.domain.vo.PayDayWorkHoursVo;
import com.ruoyi.pay.domain.bo.PayDayWorkHoursBo;
import com.ruoyi.pay.service.IPayDayWorkHoursService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 日工工时
 *
 * @author ruoyi
 * @date 2025-05-12
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/pay/dayWorkHours")
public class PayDayWorkHoursController extends BaseController {

    private final IPayDayWorkHoursService iPayDayWorkHoursService;

    /**
     * 查询日工工时列表
     */
    @SaCheckPermission("pay:dayWorkHours:list")
    @GetMapping("/list")
    public TableDataInfo<PayDayWorkHoursVo> list(PayDayWorkHoursBo bo, PageQuery pageQuery) {
        return iPayDayWorkHoursService.queryPageList2(bo, pageQuery);
    }

    /**
     * 导出日工工时列表
     */
    @SaCheckPermission("pay:dayWorkHours:export")
    @Log(title = "日工工时", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PayDayWorkHoursQuery payDayWorkHoursQuery, HttpServletResponse response) {
        List<PayDayWorkHoursQuery> list = iPayDayWorkHoursService.selectPayDayWorkHoursList2(payDayWorkHoursQuery);
        List<PayDayWorkHoursExport> result = new ArrayList<>();

        for(PayDayWorkHoursQuery query :list){
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
/*           StringBuffer sTime=new StringBuffer("");
           StringBuffer eTime=new StringBuffer("");
           StringBuffer hours=new StringBuffer("");
           StringBuffer ishelper=new StringBuffer("");
           StringBuffer salesman=new StringBuffer("");*/
            for(PayDwHoursContrast dwhours:query.getDwHoursContrasts()){
                //创建每行的对象
                PayDayWorkHoursExport export = new PayDayWorkHoursExport();
                BeanUtils.copyProperties(query,export);
                export.setStartTime(format.format(dwhours.getStartTime()));
                export.setEndTime(format.format(dwhours.getEndTime()));
                export.setHours(dwhours.getHours().doubleValue());
                if(dwhours.getIsHelper()==1){
                    export.setIsHelper("是") ;
                }else{
                    export.setIsHelper("否") ;
                }
                if(dwhours.getSalesman()==null){
                    export.setSalesman("") ;
                }else{
                    export.setSalesman(dwhours.getSalesman()) ;
                }
                result.add(export);
            }

        }
        ExcelUtil.exportExcel(result, "日工工时", PayDayWorkHoursExport.class, response);
    }

    /**
     * 获取日工工时详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("pay:dayWorkHours:query")
    @GetMapping("/{id}")
    public R<PayDayWorkHoursVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iPayDayWorkHoursService.queryById(id));
    }

    /**
     * 新增日工工时
     */
    @SaCheckPermission("pay:dayWorkHours:add")
    @Log(title = "日工工时", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PayDayWorkHoursQuery2 payDayWorkHoursQuery2) {
        return iPayDayWorkHoursService.insertByBo(payDayWorkHoursQuery2);
    }

    /**
     * 修改日工工时
     */
    @SaCheckPermission("pay:dayWorkHours:edit")
    @Log(title = "日工工时", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PayDayWorkHoursQuery bo) {
        return iPayDayWorkHoursService.updateByBo(bo);
    }

    /**
     * 删除日工工时
     *
     * @param ids 主键串
     */
    @SaCheckPermission("pay:dayWorkHours:remove")
    @Log(title = "日工工时", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iPayDayWorkHoursService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
