package com.jeeplus.test.wdt.merchantCompany.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.jeeplus.aop.logging.annotation.ApiLog;
import com.jeeplus.test.wdt.merchantCompany.service.MerchantProductService;
import com.jeeplus.test.wdt.merchantCompany.service.dto.MerchantProductDTO;
import com.jeeplus.test.wdt.merchantCompany.service.mapstuct.MerchantProductWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 入驻产品controller
 * @author: 徐滕
 * @version: 2024-01-04 15:12
 */
@Api("入驻产品管理")
@RestController
@RequestMapping(value = "/wdt/merchantProduct")
public class MerchantProductController {
    @Autowired
    private MerchantProductService merchantProductService;

    @Autowired
    private MerchantProductWrapper merchantProductWrapper;

    /**
     * 入驻产品列表数据
     */
    @ApiLog("查询入驻产品列表数据")
    @ApiOperation(value = "查询入驻产品列表数据")
    @PreAuthorize("hasAuthority('wdt:merchantProduct:list')")
    @GetMapping("list")
    public ResponseEntity<IPage<MerchantProductDTO>> list(MerchantProductDTO merchantProductDTO, Page<MerchantProductDTO> page) throws Exception {
        IPage<MerchantProductDTO> result = merchantProductService.findPage (page, merchantProductDTO);
        return ResponseEntity.ok (result);
    }

    /**
     * 根据Id获取商家基本信息
     */
    @ApiLog("根据Id获取入驻产品基本信息")
    @ApiOperation(value = "根据Id获取入驻产品基本信息")
    @PreAuthorize("hasAnyAuthority('wdt:merchantProduct:view','wdt:merchantProduct:add','wdt:merchantProduct:edit')")
    @GetMapping("queryById")
    public ResponseEntity<MerchantProductDTO> queryById(String id) {
        return ResponseEntity.ok ( merchantProductService.findById ( id ) );
    }



    /**
     * 保存请假表单
     */
    @ApiLog("保存入驻产品基本信息")
    @ApiOperation(value = "保存入驻产品基本信息")
    @PreAuthorize("hasAnyAuthority('wdt:merchantProduct:add','wdt:merchantProduct:edit')")
    @PostMapping("save")
    public ResponseEntity<String> save(@Valid @RequestBody MerchantProductDTO merchantProductDTO) {
        //新增或编辑表单保存
        merchantProductService.saveOrUpdate (merchantProductWrapper.toEntity (merchantProductDTO));
        return ResponseEntity.ok ( "保存入驻产品基本信息成功" );
    }


    /**
     * 删除请假表单
     */
    @ApiLog("删除入驻产品基本信息")
    @ApiOperation(value = "删除入驻产品基本信息")
    @PreAuthorize("hasAuthority('wdt:merchantProduct:del')")
    @DeleteMapping("delete")
    public ResponseEntity <String> delete(String ids) {
        String idArray[] = ids.split(",");
        merchantProductService.removeByIds ( Lists.newArrayList ( idArray ) );
        return ResponseEntity.ok( "删除入驻产品基本信息成功" );
    }


    /**
     * 查询入驻产品数据(大屏数据)
     */
    @ApiLog("查询入驻产品数据(大屏数据)")
    @ApiOperation(value = "查询入驻产品数据(大屏数据)")
    @GetMapping("getListJson")
    public List<Map<String,String>> getListJson(MerchantProductDTO merchantProductDTO) throws Exception {
        Map<String,Object> map = new HashMap<>();
        map.put("unit","个");
        List<Map<String,String>> mapList = Lists.newArrayList();
        List<MerchantProductDTO> listJson = merchantProductService.getListJson(merchantProductDTO);
        for (MerchantProductDTO info : listJson) {
            Map<String,String> mapInfo = new HashMap<>();
            mapInfo.put("name",info.getName());
            mapInfo.put("value",info.getSalesVolume());
            mapList.add(mapInfo);
        }
        map.put("data",mapList);
        return mapList;
    }

    /**
     * 查询入驻产品月销售额数据(大屏数据)
     */
    @ApiLog("查询入驻产品月销售额数据(大屏数据)")
    @ApiOperation(value = "查询入驻产品月销售额数据(大屏数据)")
    @GetMapping("getListMonthJson")
    public Map<String,Object> getListMonthJson(MerchantProductDTO merchantProductDTO) throws Exception {
        Map<String,Object> map = new HashMap<>();
        Map<String,Object> map1 = new HashMap<>();

        List<String> categories = Lists.newArrayList();
        List<Object> series = Lists.newArrayList();

        List<String> data = Lists.newArrayList();

        List<Map<String,String>> mapList = Lists.newArrayList();
        List<MerchantProductDTO> listJson = merchantProductService.getListJson(merchantProductDTO);
        for (MerchantProductDTO info : listJson) {
            categories.add(info.getName());
            data.add(info.getMonthlySalesVolume());
        }
        //map1.put("name","月销售额(万元)");
        map1.put("data",data);
        series.add(map1);
        map.put("categories",categories);
        map.put("series",series);
        return map;
    }

    /**
     * 查询入驻产品销售额数据(大屏数据)
     */
    @ApiLog("查询入驻产品销售额数据(大屏数据)")
    @ApiOperation(value = "查询入驻产品销售额数据(大屏数据)")
    @GetMapping("getListSaleroomJson")
    public List<Map<String,String>> getListSaleroomJson(MerchantProductDTO merchantProductDTO) throws Exception {
        Map<String,Object> map = new HashMap<>();
        List<Map<String,String>> mapList = Lists.newArrayList();
        List<MerchantProductDTO> listJson = merchantProductService.getListJson(merchantProductDTO);
        for (MerchantProductDTO info : listJson) {
            Map<String,String> mapInfo = new HashMap<>();
            mapInfo.put("name",info.getName());
            mapInfo.put("value",info.getSaleroom());
            mapInfo.put("url","");
            mapList.add(mapInfo);
        }
        map.put("data",mapList);
        return mapList;
    }
}
