package org.springblade.modules.groupon.controller;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springblade.common.enums.GrouponSourceEnum;
import org.springblade.core.excel.util.ExcelUtil;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.modules.api.response.GroupStatisResponse;
import org.springblade.modules.api.service.impl.GrouponOrderServiceImpl;
import org.springblade.modules.finance.service.IAfterSalesService;
import org.springblade.modules.finance.service.IOrderGrouponService;
import org.springblade.modules.groupon.entity.UserSelfTake;
import org.springblade.modules.groupon.service.IGrouponService;
import org.springblade.modules.groupon.service.IUserSelfTakeService;
import org.springblade.modules.groupon.vo.AppointmentVO;
import org.springblade.modules.groupon.vo.ExportAppointmentVO;
import org.springblade.modules.groupon.vo.ExportPickUpAppointmentVO;
import org.springblade.modules.groupon.vo.GrouponVO;
import org.springblade.modules.platform.service.UserAppService;
import org.springblade.modules.system.entity.UserApp;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/tz")
@Api(value = "团长后台接口", tags = "团长后台接口")
public class TzController {

    @Resource
    private IGrouponService grouponService;
    @Resource
    private GrouponOrderServiceImpl grouponOrderService;
    @Resource
    private IOrderGrouponService orderGrouponService;
    @Lazy
    @Resource
    private IAfterSalesService afterSalesService;
    @Resource
    private UserAppService userAppService;
    @Resource
    private IUserSelfTakeService userSelfTakeService;

    @GetMapping("/groupon/statis")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "查询团购统计")
    public R<GroupStatisResponse> groupStatistics() {
        GroupStatisResponse result = grouponService.groupStatistics(AuthUtil.getUserId());
        return R.data(result);
    }

    @GetMapping("/groupon-mng/list")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "查询团购管理列表")
    public R<IPage<GrouponVO>> grouponMngList(@ApiParam("state") Integer state, Query query) {
        return R.data(grouponService.grouponMngList(Condition.getPage(query), AuthUtil.getUserId(), state));
    }

    @GetMapping("/groupon-mng/detail")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "查询团购管理列表详情")
    public R<GrouponVO> grouponMngDetail(@ApiParam("拼团ID") Long groupId) {
        return R.data(grouponService.grouponMngDetail(groupId));
    }

    @PostMapping("/publishGroupon")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "新增", notes = "传入groupon")
    public R publishGroupon(@Valid @RequestBody GrouponVO groupon) {
        Assert.notBlank(groupon.getMainTitle(), "主标题不能为空");
        Assert.notBlank(groupon.getSubTitle(), "副标题不能为空");
        Assert.notEmpty(groupon.getSelfTakeIds(), "自提点不能为空");
        groupon.setSource(GrouponSourceEnum.PLATFORM.getValue());
        groupon.setOrganizerUserId(AuthUtil.getUserId());
        grouponService.publishGroupon(groupon);
        return R.status(true);
    }

    @PostMapping("verification")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "取货核销", notes = "通过取货码进行核销")
    public R verification(@ApiParam("取货码") String pickupCode){
        grouponOrderService.confirmOrder(pickupCode, AuthUtil.getUserId());
        return R.success("取货核销成功");
    }


    @GetMapping("/commander")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "获取团长团购订单")
    public R<IPage<GrouponVO>> getCommanderOrder(Query query, Integer grouponState) {
        return R.data(orderGrouponService.getCommanderOrder(Condition.getPage(query), AuthUtil.getUserId(), grouponState));
    }

    @GetMapping("/commander/detail")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "获取团长团购订单详情")
    public R<GrouponVO> getCommanderOrderDetail(@ApiParam("团购ID") Long grouponId, @ApiParam("查询类型 1-网点订单  2-全部订单") Integer queryType,
                                                @ApiParam("团购ID") Integer state) {
        return R.data(orderGrouponService.getCommanderOrderDetail(grouponId, queryType, AuthUtil.getUserId(), state));
    }

    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "同意售后")
    @PostMapping("/after-sales/agree")
    public R agree(@RequestParam("afterSalesNo") String afterSalesNo) {
        afterSalesService.agree(afterSalesNo);
        return R.success("ok");
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "驳回售后")
    @PostMapping("/after-sales/refuse")
    public R refuse(@RequestParam("afterSalesNo") String afterSalesNo, @RequestParam("refuseReason") String refuseReason) {
        Assert.notBlank(refuseReason, "拒绝原因不能为空");
        afterSalesService.refuse(afterSalesNo, refuseReason);
        return R.success("ok");
    }

    @GetMapping("/pick-up/appointment")
    @ApiOperationSupport(order = 9)
    @ApiOperation(value = "获取团长取货履约单列表")
    public R<IPage<AppointmentVO>> getPickUpAppointment(Query query, Integer state) {
        UserApp userApp = userAppService.getByUserId(AuthUtil.getUserId());
        if(ObjectUtil.isEmpty(userApp.getLivingHall()) || !userApp.getLivingHall().equals(1)){
            return R.fail("团长身份异常，请联系客服");
        }
        UserSelfTake userSelfTake = userSelfTakeService.getOne(Wrappers.lambdaQuery(UserSelfTake.class).eq(UserSelfTake::getUserId, userApp.getUserId()));
        return R.data(orderGrouponService.getPickUpAppointment(Condition.getPage(query), null, state, userSelfTake.getSelfTakeId()));
    }

    @GetMapping("/pick-up/appointment/export")
    @ApiOperationSupport(order = 10)
    @ApiOperation(value = "导出团长取货履约单")
    public void exportPickUpAppointment(Long grouponId, String grouponName, String stateDesc, String grouponStartTime, HttpServletResponse response) {
        List<ExportPickUpAppointmentVO> list = orderGrouponService.getPickUpAppointmentData(grouponId);
        for (ExportPickUpAppointmentVO e : list) {
            e.setMainTitle(grouponName);
            e.setState(stateDesc);
            e.setGrouponStartTime(grouponStartTime);
        }
        ExcelUtil.export(response, "取货履约单", "取货履约单列表", list, ExportPickUpAppointmentVO.class);
    }

    @GetMapping("/appointment/all")
    @ApiOperationSupport(order = 9)
    @ApiOperation(value = "获取运营后台履约单列表")
    public R<IPage<AppointmentVO>> getPickUpAppointmentAll(Query query, Integer state) {
        return R.data(orderGrouponService.getPickUpAppointment(Condition.getPage(query), null, state, null));
    }

    @GetMapping("/appointment/all/export")
    @ApiOperationSupport(order = 10)
    @ApiOperation(value = "导出履约单")
    public void exportPickUpAppointmentAll(Long grouponId, String grouponName, String stateDesc, String grouponStartTime,HttpServletResponse response) {
        List<ExportAppointmentVO> list = orderGrouponService.getPickUpAppointmentDataAll(grouponId);
        for (ExportAppointmentVO e : list) {
            e.setMainTitle(grouponName);
            e.setState(stateDesc);
            e.setGrouponStartTime(grouponStartTime);
        }
        ExcelUtil.export(response, "履约单", "履约单列表", list, ExportAppointmentVO.class);
    }
}
