package cn.iocoder.yudao.module.member.controller.admin.screen;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.member.controller.admin.customertraffic.vo.CustomerTrafficRespVO;
import cn.iocoder.yudao.module.member.controller.admin.screen.vo.CarTeenInfoVO;
import cn.iocoder.yudao.module.member.controller.admin.screen.vo.DishVO;
import cn.iocoder.yudao.module.member.controller.admin.screen.vo.StoreEvaluateVO;
import cn.iocoder.yudao.module.member.controller.admin.userpreference.vo.UserPreferencePercentVO;
import cn.iocoder.yudao.module.member.dal.dataobject.customertraffic.CustomerTrafficDO;
import cn.iocoder.yudao.module.member.dal.dataobject.customertraffic.CustomerTrafficDetailDO;
import cn.iocoder.yudao.module.member.service.customertraffic.CustomerTrafficService;
import cn.iocoder.yudao.module.member.service.devicewarn.DeviceWarnService;
import cn.iocoder.yudao.module.member.service.order.OrderService;
import cn.iocoder.yudao.module.member.service.orderdetail.OrderDetailService;
import cn.iocoder.yudao.module.member.service.storeevaluate.StoreEvaluateService;
import cn.iocoder.yudao.module.member.service.userpreference.UserPreferenceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * @author zt
 * @description <description class purpose>
 * @since 2024/4/16
 */
@Tag(name = "管理后台 - 大屏")
@RestController
@RequestMapping("/member/screen")
public class ScreenController {

    @Resource
    private StoreEvaluateService storeEvaluateService;
    @Resource
    private CustomerTrafficService customerTrafficService;
    @Resource
    private OrderService orderService;
    @Resource
    private OrderDetailService orderDetailService;
    @Resource
    private DeviceWarnService deviceWarnService;
    @Resource
    private UserPreferenceService preferenceService;

    @GetMapping("/getEvaluate")
    @Operation(summary = "获得店铺评价,1-好评，2-差评")
    @PermitAll
    public StoreEvaluateVO getEvaluate(Long storeId){
        return storeEvaluateService.getEvaluate(storeId);
    }

    @GetMapping("/getData")
    @Operation(summary = "食堂今日顾客流量")
    @PermitAll
    public CommonResult<List<CustomerTrafficRespVO>> getData(Long storeId) {
        List<CustomerTrafficDO> data = customerTrafficService.getData(storeId);
        return success(BeanUtils.toBean(data, CustomerTrafficRespVO.class));
    }

    @GetMapping("/half-hour")
    @Operation(summary = "食堂今日半小时刻度顾客流量")
    @PermitAll
    public CommonResult<List<CustomerTrafficDetailDO>> getHalfHour(@RequestParam(value = "storeId",required = false) Long storeId,
                                                                   @DateTimeFormat(pattern = DatePattern.NORM_DATETIME_PATTERN)
                                                                   @RequestParam(value = "start")DateTime start,
                                                                   @DateTimeFormat(pattern = DatePattern.NORM_DATETIME_PATTERN)
                                                                   @RequestParam(value = "end") DateTime end){
        return success(customerTrafficService.getHalfHour(storeId, start,end));
    }




    @GetMapping("/getUserCount")
    @Operation(summary = "获取总顾客流量")
    @PermitAll
    @Parameters({
            @Parameter(name = "storeId", description = "门店Id，不传查所有"),
            @Parameter(name = "time", description = "时间,不传查总所有,yyyy-mm-dd")
    })
    public CommonResult<Integer> getUserCount(Long storeId,String time) {
        return success(orderService.selectUser(storeId,time));
    }

    @GetMapping("/selectDishSale")
    @Operation(summary = "菜品今日销售")
    @PermitAll
    public CommonResult<List<DishVO>> selectDishSale(Long storeId) {
        return success(orderDetailService.selectDishSale(storeId));
    }

    @GetMapping("/addWarn")
    @Operation(summary = "添加设备报警次数")
    @PermitAll
    public void addWarn(HttpServletRequest request) {
        deviceWarnService.addDeviceWarn(request);
    }

    @GetMapping("/getWarn")
    @Operation(summary = "获取门店报警次数")
    @PermitAll
    public CommonResult<Long> getWarn(Long storeId) {
        return success(deviceWarnService.getWarn(storeId));
    }

    @GetMapping("/getSaleMoney")
    @Operation(summary = "销售额")
    @PermitAll
    @Parameters({
            @Parameter(name = "storeId", description = "门店Id，不传查所有门店"),
            @Parameter(name = "time", description = "时间,不传查总销售额,yyyy-mm-dd")
    })
    public CommonResult<BigDecimal> getSaleMoney(Long storeId, String time) {
        return success(orderService.getSaleMoney(storeId,time));
    }


    @GetMapping("/getCarTeenInfo")
    @Operation(summary = "获取食堂信息")
    @PermitAll
    public CommonResult<List<CarTeenInfoVO>> getCarTeenInfo() {
        return success(orderService.getCarTeenInfo());
    }

    @GetMapping("/getPreference")
    @Operation(summary = "获取偏好")
    @PermitAll
    public CommonResult<List<UserPreferencePercentVO>> getPercent() {
        return success(preferenceService.getPercent());
    }

    @GetMapping("/getTotalMoney")
    @Operation(summary = "获取当天收入和人数")
    @PermitAll
    public CommonResult<Map<String,Object>> getMoneyAndPeople(Long storeId) {
        return success(orderService.getMoneyAndPeople(storeId));
    }

}
