package com.cyksj.web.controller.manage.group;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cyksj.common.annotation.Log;
import com.cyksj.dto.Result;
import com.cyksj.enums.BusinessType;
import com.cyksj.enums.GatewayResponse;
import com.cyksj.model.entity.HomeGroup;
import com.cyksj.model.entity.OrderDon;
import com.cyksj.model.manage.views.HomeGroupView;
import com.cyksj.model.manage.views.HomeUserView;
import com.cyksj.service.mange.CmsHomeGroupService;
import com.cyksj.service.mange.CmsOrderDonService;
import com.ejlchina.searcher.BeanSearcher;
import com.ejlchina.searcher.SearchResult;
import com.ejlchina.searcher.util.MapUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author chan
 * @date 2022/10/12 11:54 AM
 */
@Slf4j
@RequestMapping("/manage/home")
@RequiredArgsConstructor
@RestController
public class CmsHomeGroupController {

    private final BeanSearcher beanSearcher;

    private final HttpServletRequest request;

    private final CmsHomeGroupService homeGroupService;

    private final CmsOrderDonService orderDonService;



    @GetMapping("/get")
    public Result<List<HomeGroupView>> get() {
        List<HomeGroupView> homeGroupViews = beanSearcher.searchAll(HomeGroupView.class, MapUtils.flat(request.getParameterMap()));
        return GatewayResponse.SUCCESS.newBuilder().toResult(homeGroupViews);
    }

    @PutMapping("/update")
    public Result<String> update(@RequestBody HomeGroup homeGroup) {
        homeGroupService.updateById(homeGroup);
        return GatewayResponse.SUCCESS.newBuilder().toResult();
    }


    @Transactional(rollbackFor = Throwable.class)
    @PostMapping("/post")
    @Log(module = "平台管理/家庭组管理新增用户", businessType = BusinessType.POST, isSaveRequestData = true)
    public Result<String> post(@RequestBody HomeGroup homeGroup) {
        OrderDon orderDon = orderDonService.getOne(Wrappers.lambdaQuery(OrderDon.class)
                .eq(OrderDon::getUserId, homeGroup.getUserId())
                .eq(OrderDon::getRelationId, homeGroup.getRelationId())
                .eq(OrderDon::getStatus,OrderDon.Status.hasPayment)
                .orderByDesc(OrderDon::getId)
                .last(" limit 1")
        );
        if(orderDon != null){
            log.info("添加家庭组 修改 订单状态 为已完成 orderId:{}",orderDon.getId());
            orderDon.setStatus(OrderDon.Status.complete);
            orderDonService.updateById(orderDon);
        }

        homeGroupService.save(homeGroup);
        return GatewayResponse.SUCCESS.newBuilder().toResult();
    }

    @DeleteMapping("/delete/{id}")
    @Log(module = "平台管理/家庭组管理删除用户", businessType = BusinessType.DELETE, isSaveRequestData = true)
    public Result<String> delete(@PathVariable Long id) {
        homeGroupService.removeById(id);
        return GatewayResponse.SUCCESS.newBuilder().toResult();
    }


    @GetMapping("/get/user/unInvited")
    public Result<SearchResult<HomeUserView>> getUser() {
        SearchResult<HomeUserView> search = beanSearcher.search(HomeUserView.class, MapUtils.flatBuilder(request.getParameterMap()).build());
        return GatewayResponse.SUCCESS.newBuilder().toResult(search);
    }

}
