package com.czg.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import com.czg.account.dto.HandoverRecordDTO;
import com.czg.account.entity.HandoverRecord;
import com.czg.account.service.HandoverRecordService;
import com.czg.account.vo.HandoverProductListVo;
import com.czg.account.vo.HandoverTotalVo;
import com.czg.annotation.SaAdminCheckPermission;
import com.czg.excel.ExcelExportUtil;
import com.czg.log.annotation.OperationLog;
import com.czg.resp.CzgResult;
import com.mybatisflex.core.paginate.Page;
import jakarta.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 交班
 *
 * @author Tankaikai tankaikai@aliyun.com
 * @since 1.0 2025-02-27
 */
@AllArgsConstructor
@RestController
@RequestMapping("/admin/handoverRecord")
public class HandoverRecordController {
    private final HandoverRecordService handoverRecordService;

    /**
     * 交班记录-分页
     *
     * @param beginDate 开始时间 格式：yyyy-MM-dd
     * @param endDate   结束时间 格式：yyyy-MM-dd
     */
    @GetMapping("page")
    @SaAdminCheckPermission(parentName = "交班记录", value = "handoverRecord:page", name = "交班记录-分页")
    public CzgResult<Page<HandoverRecordDTO>> page(@RequestParam(required = false) String beginDate, @RequestParam(required = false) String endDate) {
        Page<HandoverRecordDTO> page = handoverRecordService.getHandoverRecordPage(beginDate, endDate);
        return CzgResult.success(page);
    }

    /**
     * 交班记录-详情
     *
     * @param id 交班记录ID
     */
    @GetMapping("/detail/{id}")
    @SaAdminCheckPermission(parentName = "交班记录", value = "handoverRecord:detail", name = "交班记录-详情")
    public CzgResult<HandoverRecordDTO> detail(@PathVariable Long id) {
        HandoverRecord entity = handoverRecordService.getById(id);
        HandoverRecordDTO data = BeanUtil.copyProperties(entity, HandoverRecordDTO.class);
        return CzgResult.success(data);
    }


    /**
     * 交班记录-商品详情
     *
     * @param id 交班记录ID
     */
    @GetMapping("{id}")
    @SaAdminCheckPermission(parentName = "交班记录", value = "handoverRecord:detail", name = "交班记录-详情")
    public CzgResult<List<HandoverProductListVo>> info(@PathVariable Long id) {
        List<HandoverProductListVo> data = handoverRecordService.getHandoverProductListById(id);
        return CzgResult.success(data);
    }

    /**
     * 交班记录-商品详情导出
     *
     * @param id 交班记录ID
     */
    @GetMapping("/export/{id}")
    @OperationLog("交班记录-导出")
    @SaAdminCheckPermission(parentName = "交班记录", value = "handoverRecord:export", name = "交班记录-导出")
    public void export(@PathVariable Long id, HttpServletResponse response) {
        List<HandoverProductListVo> list = handoverRecordService.getHandoverProductListById(id);
        ExcelExportUtil.exportToResponse(list, HandoverProductListVo.class, "交班售出商品明细", response);
    }


    /**
     * 收银机-实时交班数据
     */
    @GetMapping("total")
    @SaAdminCheckPermission(parentName = "交班记录", value = "handoverRecord:realTime", name = "收银机-实时交班数据")
    public CzgResult<HandoverTotalVo> realTime() {
        HandoverTotalVo data = handoverRecordService.totalHandoverData();
        return CzgResult.success(data);
    }

    /**
     * 收银机-交班/关班
     *
     * @param isPrint 是否打印交班小票
     */
    @PostMapping("handover")
    @OperationLog("收银机-交班/关班")
    @SaAdminCheckPermission(parentName = "交班记录", value = "handoverRecord:handover", name = "收银机-交班/关班")
    public CzgResult<Long> handover(@RequestParam Integer isPrint) {
        HandoverRecord handoverRecord = handoverRecordService.handover();
        handoverRecordService.printHandoverReceipt(handoverRecord, isPrint);
        return CzgResult.success(handoverRecord.getId());
    }
}
