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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.core.excel.EasyExcelUtils;
import com.jeeplus.core.excel.ExcelOptions;
import com.jeeplus.core.excel.annotation.ExportMode;
import com.jeeplus.core.query.QueryWrapperGenerator;
import com.jeeplus.sys.domain.DictValue;
import com.jeeplus.sys.service.dto.DictValueDTO;
import com.jeeplus.sys.utils.DictUtils;
import com.jeeplus.test.utils.excel.MerchantCompanyEasyExcel;
import com.jeeplus.test.wdt.merchantCompany.service.MerchantCompanyService;
import com.jeeplus.test.wdt.merchantCompany.service.dto.*;
import com.jeeplus.test.wdt.merchantCompany.service.mapstuct.MerchantCompanyWrapper;
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 org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 入驻商家controller
 * @author: 徐滕
 * @version: 2024-01-04 10:40
 */

@Api("入驻商家管理")
@RestController
@RequestMapping(value = "/wdt/merchantCompany")
public class MerchantCompanyController {
    @Autowired
    private MerchantCompanyService merchantCompanyService;

    @Autowired
    private MerchantCompanyWrapper merchantCompanyWrapper;

    /**
     * 入驻商家列表数据
     */
    @ApiLog("查询入驻商家列表数据")
    @ApiOperation(value = "查询入驻商家列表数据")
    @PreAuthorize("hasAuthority('wdt:merchantCompany:list')")
    @GetMapping("list")
    public ResponseEntity<IPage<MerchantCompanyDTO>> list(MerchantCompanyDTO merchantCompanyDTO, Page<MerchantCompanyDTO> page) throws Exception {
        IPage<MerchantCompanyDTO> result = merchantCompanyService.findPage (page, merchantCompanyDTO);
        return ResponseEntity.ok (result);
    }

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



    /**
     * 保存商家基本信息
     */
    @ApiLog("保存商家基本信息")
    @ApiOperation(value = "保存商家基本信息")
    @PreAuthorize("hasAnyAuthority('wdt:merchantCompany:add','wdt:merchantCompany:edit')")
    @PostMapping("save")
    public  ResponseEntity <String> save(@Valid @RequestBody MerchantCompanyDTO merchantCompanyDTO) {
        //新增或编辑表单保存
        merchantCompanyService.saveOrUpdate (merchantCompanyDTO);
        return ResponseEntity.ok ( "保存商家基本信息成功" );
    }


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



    /**
     * 导入入驻商家数据
     *
     * @return
     */
    @PreAuthorize("hasAnyAuthority('wdt:merchantCompany:import')")
    @PostMapping("import")
    public ResponseEntity importFile(MultipartFile file) throws IOException {
        String result = MerchantCompanyEasyExcel.newInstance ( merchantCompanyService, MerchantCompanyWrapper.INSTANCE ).importExcel ( file, MerchantCompanyDTO.class );
        return ResponseEntity.ok ( result );
    }

    /**
     * 下载导入入驻商家数据模板
     *
     * @param response
     * @return
     */
    @PreAuthorize ("hasAnyAuthority('wdt:merchantCompany:import')")
    @GetMapping("import/template")
    public void importFileTemplate(HttpServletResponse response) throws IOException {
        String fileName = "入驻商家数据导入模板.xlsx";
        List<MerchantCompanyDTO> list = Lists.newArrayList();
        MerchantCompanyDTO info = new MerchantCompanyDTO();
        info.setCompanyName("测试公司名称");
        info.setCompanyType("产品加工企业/合作企业");
        info.setLinkmanName("测试联系人名称");
        info.setLinkmanPhone("138xxxx8888");
        info.setCommerceShop("是/否");
        info.setPractitionerCount(15);
        info.setAnnualSales("100000");
        info.setOnlineRetailSales("200000");
        SettledPlatformDTO settledPlatformDTO = new SettledPlatformDTO();
        settledPlatformDTO.setName("抖音");
        SettledPlatformDTO settledPlatformDTO1 = new SettledPlatformDTO();
        settledPlatformDTO1.setName("淘宝");
        List<SettledPlatformDTO> settledPlatformDTOList = Lists.newArrayList();
        settledPlatformDTOList.add(settledPlatformDTO);
        settledPlatformDTOList.add(settledPlatformDTO1);
        info.setSettledPlatformDTOList(settledPlatformDTOList);

        MerchantProductDTO merchantProductDTO = new MerchantProductDTO();
        merchantProductDTO.setName("农产品");
        MerchantProductDTO merchantProductDTO1 = new MerchantProductDTO();
        merchantProductDTO1.setName("手工产品");
        List<MerchantProductDTO> merchantProductDTOList = Lists.newArrayList();
        merchantProductDTOList.add(merchantProductDTO);
        merchantProductDTOList.add(merchantProductDTO1);
        info.setMerchantProductDTOList(merchantProductDTOList);
        list.add (info);
        EasyExcelUtils.newInstance ( merchantCompanyService, merchantCompanyWrapper ).exportExcel ( list,  "入驻商家数据", MerchantCompanyDTO.class, fileName, null, response );
    }


    /**
     * 导出入驻商家数据
     *
     * @param merchantCompanyDTO
     * @param page
     * @param response
     * @throws Exception
     */
    @ApiLog("导出入驻商家数据")
    @PreAuthorize("hasAnyAuthority('wdt:merchantCompany:export')")
    @GetMapping("export")
    public void exportFile(MerchantCompanyDTO merchantCompanyDTO, Page <MerchantCompanyDTO> page, ExcelOptions options, HttpServletResponse response) throws Exception {
        String fileName = options.getFilename ( );
        QueryWrapper queryWrapper = QueryWrapperGenerator.buildQueryCondition (merchantCompanyDTO, MerchantCompanyDTO.class);
        List<MerchantCompanyDTO> result;
        if (ExportMode.current.equals(options.getMode())) {
            result = merchantCompanyService.findPage(page, merchantCompanyDTO).getRecords();
        } else if (ExportMode.selected.equals(options.getMode())) {
            result = merchantCompanyService.findPage(page, merchantCompanyDTO).getRecords().stream().filter(merchantCompanyDTO1 ->
                    options.getSelectIds().contains(merchantCompanyDTO1.getId())
            ).collect(Collectors.toList());
        } else {
            page.setSize(-1);
            page.setCurrent(0);
            result = merchantCompanyService.findPage(page, merchantCompanyDTO).getRecords();
        }
        EasyExcelUtils.newInstance ( merchantCompanyService, merchantCompanyWrapper ).exportExcel ( result,  options.getSheetName ( ), MerchantCompanyExportDTO.class, fileName,options.getExportFields (), response );
    }


    /**
     * 入驻商家列表数据(大屏数据)
     */
    @ApiLog("查询入驻商家列表数据(大屏数据)")
    @ApiOperation(value = "查询入驻商家列表数据(大屏数据)")
    @GetMapping("getListJson")
    public List<MerchantCompanyScreenDTO> getListJson(MerchantCompanyDTO merchantCompanyDTO) throws Exception {
        List<MerchantCompanyScreenDTO> result = merchantCompanyService.getListJson (merchantCompanyDTO);
        return result;
    }

}
