package com.jwipc.sys.controller.pc;

import com.alibaba.excel.EasyExcel;
import com.jwipc.page.PageView;
import com.jwipc.security.LoginUtil;
import com.jwipc.sys.dto.PurchaseGroupDTO;
import com.jwipc.sys.dto.PurchaseGroupFactoryExportDTO;
import com.jwipc.sys.dto.UserPurGroupTreeDTO;
import com.jwipc.sys.dto.UserPurchaseGroupUpdateDTO;
import com.jwipc.sys.logic.PurchaseGroupLogic;
import com.jwipc.sys.model.param.PurchaseGroupParam;
import com.jwipc.sys.model.query.PurchaseGroupFactoryQuery;
import com.jwipc.sys.model.query.PurchaseGroupQuery;
import com.jwipc.sys.model.query.UserPurchaseGroupUpdateQuery;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

import static com.jwipc.sys.constant.ModuleConst.MODULE_ROOT;

/**
 * @author Al
 * @create 2024-01-29 16:22
 */
@RestController
@RequestMapping(MODULE_ROOT + "/purchaseGroup/rest/")
@RequiredArgsConstructor
public class PurchaseGroupRestController {
    private final PurchaseGroupLogic purchaseGroupLogic;

    /**
     * 列表
     *
     * @param query
     * @return
     */
    @RequestMapping("/list")
    public PageView list(PurchaseGroupQuery query) {
        return purchaseGroupLogic.findByPage(query);
    }

    /**
     * 列表
     *
     * @param query
     * @return
     */
    @RequestMapping("/listAll")
    public ResponseEntity<List<PurchaseGroupDTO>> listAll(PurchaseGroupQuery query) {
        if (query.getIsAuthority()!=null){
            query.setUserId(query.getIsAuthority()==1? LoginUtil.getUserId():null);
        }
        return ResponseEntity.ok(purchaseGroupLogic.listAll(query));
    }

    /**
     * 创建采购组
     *
     * @param query
     * @return
     */
    @RequestMapping("/createListAll")
    public ResponseEntity<List<UserPurchaseGroupUpdateDTO>> createListAll(UserPurchaseGroupUpdateQuery query) {
//        if (query.getIsAuthority()!=null){
//            query.setUserId(query.getIsAuthority()==1? LoginUtil.getUserId():null);
//        }
        return ResponseEntity.ok(purchaseGroupLogic.createListAll(query));
    }

    /**
     * 保存更新
     *
     * @param param 参数
     */
    @PostMapping("/saveOrUpdate")
    public ResponseEntity saveOrUpdate(PurchaseGroupParam param) {
        purchaseGroupLogic.saveOrUpdate(param);
        return ResponseEntity.ok(true);
    }

    /**
     * 删除
     *
     * @param purchaseGroupCode 参数
     */
    @PostMapping("/remove")
    public ResponseEntity remove(String purchaseGroupCode) {
        purchaseGroupLogic.remove(purchaseGroupCode);
        return ResponseEntity.ok(true);
    }

    @GetMapping("getForTree")
    public ResponseEntity getForTree(Long userId) {
        List<UserPurGroupTreeDTO> list = purchaseGroupLogic.getForTree(userId);
        return ResponseEntity.ok(list);
    }

    @GetMapping("factoryList")
    public PageView factoryList(PurchaseGroupFactoryQuery query) {
        return purchaseGroupLogic.getFactoryList(query);
    }

    @PostMapping("updateFactoryStatus")
    public ResponseEntity updateFactoryStatus(String ids, Integer status) {
        purchaseGroupLogic.updateFactoryStatus(ids,status);
        return ResponseEntity.ok().build();
    }

    @PostMapping("factoryImportData")
    public ResponseEntity factoryImportData(@RequestParam(value = "file") MultipartFile file) {
        purchaseGroupLogic.factoryImportData(file);
        return ResponseEntity.ok().build();
    }

    @GetMapping("factoryExportData")
    public void factoryExportData(HttpServletResponse response, PurchaseGroupFactoryQuery query) throws IOException {
        // 创建数据列表
        final List<PurchaseGroupFactoryExportDTO> forExport = purchaseGroupLogic.getFactoryForExport(query);

        // 设置响应头信息
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("采购组工厂数据", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        // 使用EasyExcel导出Excel文件
        EasyExcel.write(response.getOutputStream(), PurchaseGroupFactoryExportDTO.class).sheet("Sheet1")
                .doWrite(forExport);
    }
}
