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

import cn.hutool.core.collection.CollectionUtil;
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.business.vo.BusinessRespVO;
import cn.iocoder.yudao.module.member.controller.admin.customertraffic.vo.CustomerTrafficRespVO;
import cn.iocoder.yudao.module.member.controller.admin.screen.vo.*;
import cn.iocoder.yudao.module.member.controller.admin.userpreference.vo.UserPreferencePercentVO;
import cn.iocoder.yudao.module.member.dal.dataobject.business.BusinessDO;
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.business.BusinessService;
import cn.iocoder.yudao.module.member.service.customertraffic.CustomerTrafficService;
import cn.iocoder.yudao.module.member.service.devicewarn.DeviceWarnNumService;
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.storebusiness.StoreBusinessService;
import cn.iocoder.yudao.module.member.service.storeevaluate.StoreEvaluateService;
import cn.iocoder.yudao.module.member.service.storeorderdetail.StoreOrderDetailService;
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.*;

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 java.util.stream.Collectors;

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 DeviceWarnNumService deviceWarnService;
    @Resource
    private UserPreferenceService preferenceService;
    @Resource
    private BusinessService businessService;
    @Resource
    private StoreBusinessService storeBusinessService;
    @Resource
    private StoreOrderDetailService storeOrderDetailService;

    @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("/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));
    }


    @GetMapping("/totalBusiness")
    @Operation(summary = "营业数据")
    @PermitAll
    public CommonResult<BusinessDO> getTotalBusiness(Long storeId) {
        return success(businessService.getToday(storeId));
    }

    @GetMapping("/deviceInfo")
    @Operation(summary = "设备数据")
    @PermitAll
    public CommonResult<List<DeviceInfo>> getDeviceInfo(Long storeId) {
        List<DeviceInfo> deviceInfo = deviceWarnService.getDeviceInfo(storeId);
        for (DeviceInfo info : deviceInfo) {
            info.setDiff(info.getTotalWeight().subtract(info.getRemWeight()));
        }
        return success(deviceInfo);
    }

    @GetMapping("/warnDish")
    @Operation(summary = "告警菜品")
    @PermitAll
    public CommonResult<List<DeviceInfo>> getWarnDish(Long storeId) {
        List<DeviceInfo> deviceInfo = deviceWarnService.getDeviceInfo(storeId);
        if(CollectionUtil.isNotEmpty(deviceInfo)){
            deviceInfo = deviceInfo.stream().filter(deviceInfo1 ->
                    deviceInfo1.getRemWeight().compareTo(deviceInfo1.getFirstWeight().multiply(new BigDecimal("0.2")))<0)
                    .collect(Collectors.toList());
        }
        return success(deviceInfo);
    }

    @GetMapping("/turnover")
    @Operation(summary = "获取食堂营业额")
    @PermitAll
    public CommonResult<BusinessRespVO> getCanteenTurnover(TurnoverVO  turnoverVO) {
        return success(businessService.getTurnover(turnoverVO));
    }

    @GetMapping("/turnoverHistogram")
    @Operation(summary = "获取食堂营业额柱状图")
    @PermitAll
    public CommonResult<List<BusinessRespVO>> getCanteenTurnoverHistogram(TurnoverVO  turnoverVO) {
        return success(businessService.getTurnoverList(turnoverVO));
    }

    @GetMapping("/selectDishSale")
    @Operation(summary = "菜品销量")
    @PermitAll
    public CommonResult<List<DishVO>> selectDishSale(Long storeId,String startTime,String endTime,Integer type) {
        return success(orderDetailService.selectDishSale(storeId,startTime,endTime,type));
    }

    @GetMapping("/storeTurnover")
    @Operation(summary = "获取超市营业额")
    @PermitAll
    public CommonResult<BusinessRespVO> getStoreTurnover(TurnoverVO  turnoverVO) {
        return success(storeBusinessService.getTurnover(turnoverVO));
    }

    @GetMapping("/storeTurnoverHistogram")
    @Operation(summary = "获取超市营业额柱状图")
    @PermitAll
    public CommonResult<List<BusinessRespVO>> getStoreTurnoverHistogram(TurnoverVO  turnoverVO) {
        return success(storeBusinessService.getTurnoverList(turnoverVO));
    }


    @GetMapping("/selectGoodsSale")
    @Operation(summary = "商品销量")
    @PermitAll
    public CommonResult<List<GoodsVO>> selectGoodsSale(Long storeId,String startTime,String endTime,Integer type) {
        return success(storeOrderDetailService.selectGoodsSale(storeId,startTime,endTime,type));
    }
}
