package com.ruoyi.pay.controller;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.pay.domain.PaySalaryReport;
import com.ruoyi.pay.domain.vo.ExportBaohuoVo;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
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.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.pay.domain.vo.PayWorkOrderBaohuoVo;
import com.ruoyi.pay.domain.bo.PayWorkOrderBaohuoBo;
import com.ruoyi.pay.service.IPayWorkOrderBaohuoService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 包活派工单
 *
 * @author ruoyi
 * @date 2025-05-12
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/pay/workOrderBaohuo")
public class PayWorkOrderBaohuoController extends BaseController {

    private final IPayWorkOrderBaohuoService iPayWorkOrderBaohuoService;

    /**
     * 查询包活派工单列表
     */
    @SaCheckPermission("pay:workOrderBaohuo:list")
    @GetMapping("/list")
    public TableDataInfo<PayWorkOrderBaohuoVo> list(PayWorkOrderBaohuoBo bo, PageQuery pageQuery) {
        bo.setDeleted(0);
        if (ObjectUtil.isNotNull(bo.getWorkDate()) || ObjectUtil.isNotNull(bo.getEmpId())) {
            HashSet<Long> woids = iPayWorkOrderBaohuoService.getWoIdSet(bo.getWorkDate(), bo.getEmpId());
            if (woids.isEmpty()) {
                return TableDataInfo.build(new ArrayList<>());
            }
            bo.setWoids(new ArrayList<>(woids));
        }
        return iPayWorkOrderBaohuoService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询员工总工时
     */
    @SaCheckPermission("pay:workOrderBaohuo:sum")
    @GetMapping("/sum")
    public R<List<PaySalaryReport>> sum(@RequestParam(name = "date")String date,
                                        @RequestParam(name = "deptId", required = false) Long deptId,
                                        @RequestParam(name = "tenantId") Long tenantId
    ) {
        return iPayWorkOrderBaohuoService.baohuoSum(date, deptId,tenantId);
    }

    /**
     * 导出包活派工单列表
     */
    @SaCheckPermission("pay:workOrderBaohuo:export")
    @Log(title = "包活派工单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@RequestParam(name = "exportDate") String exportDate,
                       @RequestParam(name = "tenantId") Long tenantId, HttpServletResponse response) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Date exportDate1 = sdf.parse(exportDate);
        List<ExportBaohuoVo> list = iPayWorkOrderBaohuoService.getWorkOrderListByDate(tenantId,exportDate1);
        list.stream().parallel().forEach(vo -> {
            vo.setAvgAmount(vo.getAmount().divide(new BigDecimal(vo.getWorkerNum()), 2, RoundingMode.HALF_UP));
            vo.setWorkTime(vo.getWorkTime().divide(new BigDecimal(vo.getWorkerNum()), 2,RoundingMode.HALF_UP));
        });
        ExcelUtil.exportExcel(list, "包活派工单", ExportBaohuoVo.class, response);
    }

    /**
     * 获取包活派工单详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("pay:workOrderBaohuo:query")
    @GetMapping("/{id}")
    public R<PayWorkOrderBaohuoBo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iPayWorkOrderBaohuoService.queryById(id));
    }

    /**
     * 新增包活派工单
     */
    @SaCheckPermission("pay:workOrderBaohuo:add")
    @Log(title = "包活派工单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PayWorkOrderBaohuoBo bo) {
        return toAjax(iPayWorkOrderBaohuoService.insertByBo(bo));
    }

    /**
     * 修改包活派工单
     */
    @SaCheckPermission("pay:workOrderBaohuo:edit")
    @Log(title = "包活派工单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PayWorkOrderBaohuoBo bo) {
        return toAjax(iPayWorkOrderBaohuoService.updateByBo(bo));
    }

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