package com.jsowell.web.controller.pile;

import com.alibaba.fastjson2.JSON;
import com.jsowell.common.annotation.Log;
import com.jsowell.common.core.controller.BaseController;
import com.jsowell.common.core.domain.AjaxResult;
import com.jsowell.common.core.page.TableDataInfo;
import com.jsowell.common.enums.BusinessType;
import com.jsowell.common.exception.BusinessException;
import com.jsowell.common.util.poi.ExcelUtil;
import com.jsowell.pile.domain.MemberGroup;
import com.jsowell.pile.dto.MemberGroupDTO;
import com.jsowell.pile.service.MemberGroupService;
import com.jsowell.pile.util.UserUtils;
import com.jsowell.pile.vo.base.LoginUserDetailVO;
import com.jsowell.pile.vo.base.MerchantInfoVO;
import com.jsowell.pile.vo.web.MemberGroupVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 会员组Controller
 *
 * @author jsowell
 * @date 2023-12-26
 */
@RestController
@RequestMapping("/member/memberGroup")
public class MemberGroupController extends BaseController {
    @Autowired
    private MemberGroupService memberGroupService;

    /**
     * 查询会员组列表
     */
    @PreAuthorize("@ss.hasPermi('member:memberGroup:list')")
    @GetMapping("/list")
    public TableDataInfo list(MemberGroupDTO memberGroup) {
        // LoginUserDetailVO loginUserDetail = UserUtils.getLoginUserDetail();
        // logger.info("获取登录用户信息:{}", JSON.toJSONString(loginUserDetail));
        List<MerchantInfoVO> merchantInfoVOList = UserUtils.getMerchantInfoVOList();
        List<String> collect = merchantInfoVOList.stream().map(MerchantInfoVO::getMerchantId).collect(Collectors.toList());
        memberGroup.setMerchantIdList(collect);
        startPage();
        List<MemberGroupVO> list = memberGroupService.selectMemberGroupList(memberGroup);
        return getDataTable(list);
    }

    /**
     * 导出会员组列表
     */
    @PreAuthorize("@ss.hasPermi('member:memberGroup:export')")
    @Log(title = "会员组", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MemberGroupDTO memberGroup) {
        List<MemberGroupVO> list = memberGroupService.selectMemberGroupList(memberGroup);
        ExcelUtil<MemberGroup> util = new ExcelUtil<MemberGroup>(MemberGroup.class);
        // util.exportExcel(response, list, "会员组数据");
    }

    /**
     * 获取会员组详细信息
     */
    @PreAuthorize("@ss.hasPermi('member:memberGroup:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(memberGroupService.selectMemberGroupById(id));
    }

    /**
     * 新增会员组
     */
    @PreAuthorize("@ss.hasPermi('member:memberGroup:add')")
    @Log(title = "会员组", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MemberGroup memberGroup) {
        AjaxResult ajaxResult;
        try {
            int i = memberGroupService.insertMemberGroup(memberGroup);
            if (i > 0) {
                ajaxResult = AjaxResult.success();
            } else {
                ajaxResult = AjaxResult.error();
            }
        } catch (BusinessException e) {
            logger.error("创建运营商集团异常", e);
            ajaxResult = AjaxResult.error(e.getMessage());
        } catch (Exception e) {
            logger.error("创建运营商集团失败", e);
            ajaxResult = AjaxResult.error("创建运营商集团失败");
        }
        return ajaxResult;
    }

    /**
     * 修改会员组
     */
    @PreAuthorize("@ss.hasPermi('member:memberGroup:edit')")
    @Log(title = "会员组", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MemberGroup memberGroup) {
        return toAjax(memberGroupService.updateMemberGroup(memberGroup));
    }

    /**
     * 删除会员组
     */
    @PreAuthorize("@ss.hasPermi('member:memberGroup:remove')")
    @Log(title = "会员组", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(memberGroupService.deleteMemberGroupByIds(ids));
    }

    /**
     * 会员组添加会员, 后管添加
     */
    @PostMapping("/addMember")
    public AjaxResult addMember(@RequestBody MemberGroupDTO dto) {
        return toAjax(memberGroupService.addMember(dto));
    }

    /**
     * 查询会员组中会员列表
     */
    @GetMapping("/queryMemberList")
    public TableDataInfo queryMemberList(MemberGroupDTO dto) {
        startPage();
        return getDataTable(memberGroupService.queryMemberList(dto));
    }

    /**
     * 查询会员组/集团 的站点列表
     */
    @GetMapping("/queryStationList")
    public TableDataInfo queryStationList(MemberGroupDTO dto) {
        startPage();
        return getDataTable(memberGroupService.queryStationList(dto));
    }

    /**
     * 集团组移除站点
     */
    @PostMapping("/removeStationFromMemberGroup")
    public AjaxResult removeStationFromMemberGroup(@RequestBody MemberGroupDTO dto) {
        return toAjax(memberGroupService.removeStationFromMemberGroup(dto));
    }

    /**
     * 集团移除会员
     */
    @PostMapping("/removeMemberFromMemberGroup")
    public AjaxResult removeMemberFromMemberGroup(@RequestBody MemberGroupDTO dto) {
        return toAjax(memberGroupService.removeMemberFromMemberGroup(dto));
    }

    /**
     * 根据memberGroup查询车辆消费记录
     * /member/memberGroup/queryMemberGroupConsumptionList
     */
    @PostMapping("/queryMemberGroupConsumptionList")
    public TableDataInfo queryMemberGroupConsumptionList(@RequestBody MemberGroupDTO dto) {
        return getDataTable(memberGroupService.queryMemberGroupConsumptionList(dto.getGroupCode()));
    }

    /**
     * 查询车辆消费明细
     * /member/memberGroup/queryConsumptionDetailListByVinCode
     */
    @PostMapping("/queryConsumptionDetailListByVinCode")
    public TableDataInfo queryConsumptionDetailListByVinCode(@RequestBody MemberGroupDTO dto) {
        return getDataTable(memberGroupService.queryConsumptionDetailListByVinCode(dto.getGroupCode(), dto.getVinCode()));
    }
}
