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.SaStaffCheckPermission;
import com.czg.config.RabbitPublisher;
import com.czg.enums.YesNoEnum;
import com.czg.log.annotation.OperationLog;
import com.czg.resp.CzgResult;
import com.mybatisflex.core.paginate.Page;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import jakarta.annotation.Resource;
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;
    @Resource
    private RabbitPublisher rabbitPublisher;

    /**
     * 交班记录-分页
     *
     * @param beginDate 开始时间 格式：yyyy-MM-dd
     * @param endDate   结束时间 格式：yyyy-MM-dd
     */
    @GetMapping("page")
    @OperationLog("交班记录-分页")
    @SaStaffCheckPermission("yun_xu_cha_kan_suo_you_jiao_ban_ji_lu")
    //@SaAdminCheckPermission("handoverRecord:page")
    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("{id}")
    @OperationLog("交班记录-查看")
    @SaStaffCheckPermission("yun_xu_cha_kan_suo_you_jiao_ban_ji_lu")
    //@SaAdminCheckPermission("handoverRecord:info")
    public CzgResult<List<HandoverProductListVo>> info(@PathVariable Long id) {
        List<HandoverProductListVo> data = handoverRecordService.getHandoverProductListById(id);
        return CzgResult.success(data);
    }

    /**
     * 交班记录-导出
     *
     * @param id 交班记录ID
     */
    @ResponseExcel(name = "交班售出商品明细")
    @GetMapping("/export/{id}")
    @OperationLog("交班记录-导出")
    @SaStaffCheckPermission("yun_xu_cha_kan_suo_you_jiao_ban_ji_lu")
    //@SaAdminCheckPermission("handoverRecord:export")
    public List<HandoverProductListVo> export(@PathVariable Long id) {
        return handoverRecordService.getHandoverProductListById(id);
    }

    /**
     * 收银机-交班数据统计
     */
    @GetMapping("total")
    @OperationLog("收银机-交班数据统计")
    @SaStaffCheckPermission("yun_xu_cha_kan_suo_you_jiao_ban_ji_lu")
    //@SaAdminCheckPermission("handoverRecord:total")
    public CzgResult<HandoverTotalVo> total() {
        HandoverTotalVo data = handoverRecordService.totalHandoverData();
        return CzgResult.success(data);
    }

    /**
     * 收银机-交班/关班
     *
     * @param isPrint 是否打印交班小票
     */
    @PostMapping("handover")
    @OperationLog("收银机-交班/关班")
    @SaStaffCheckPermission("yun_xu_jiao_ban")
    //@SaAdminCheckPermission("handoverRecord:handover")
    public CzgResult<Long> handover(@RequestParam Integer isPrint) {
        Long id = handoverRecordService.handover();
        return CzgResult.success(id);
    }

    /**
     * 收银机-交班/关班-网络打印机打印交班小票
     *
     * @param id 交班记录id
     */
    @PostMapping("/network/print/{id}")
    @OperationLog("收银机-交班/关班-网络打印机打印交班小票")
    @SaStaffCheckPermission("yun_xu_cha_kan_suo_you_jiao_ban_ji_lu")
    //@SaAdminCheckPermission("handoverRecord:networkPrint")
    public CzgResult<Void> handover(@PathVariable Long id) {
        handoverRecordService.printHandoverReceipt(id, YesNoEnum.YES.value());
        return CzgResult.success();
    }


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

}
