package com.jwipc.supplier.controller.pc;

import com.alibaba.fastjson2.JSON;
import com.jwipc.supplier.dto.*;
import com.jwipc.supplier.logic.RegisterLogic;
import com.jwipc.sys.client.dto.ValidationRuleSettingInfoClientDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

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

/**
 * <p> </p>
 *
 * @author ZhangJie
 * @version 1.0
 * @since JDK 1.8
 */
@RestController
@RequestMapping(MODULE_ROOT + "/register/rest")
@RequiredArgsConstructor
public class RegisterRestController {
    private final RegisterLogic registerLogic;

    /**
     * 供应商注册页面上字典值数据获取
     */
    @PostMapping("dict/data")
    public ResponseEntity registerDictData(String query) {
        SupplierRegisterQueryDTO registerQuery = JSON.parseObject(query, SupplierRegisterQueryDTO.class);
        return ResponseEntity.ok(registerLogic.registerDictData(registerQuery));
    }

    /**
     * 供应商注册页面上字典值数据获取
     */
    @PostMapping("auto/save")
    public ResponseEntity autoSave(RegisterAutoSaveDTO autoSaveDTO) {
        registerLogic.autoSave(autoSaveDTO);
        return ResponseEntity.ok().build();
    }

    /**
     * 供应商相关关系表保存
     */
    @PostMapping("save/follow")
    public ResponseEntity saveSupplierFollow(String supplierFollow, Integer step) {
        RegisterSaveDTO followTable = JSON.parseObject(supplierFollow, RegisterSaveDTO.class);
        final String supplierId = registerLogic.followTableSave(followTable, step);
        return ResponseEntity.ok(supplierId);
    }

    /**
     * 根据产品编码获取对应的json 数据
     *
     * @param query creditQuery的json数组，{code: "E_007_0601", requestParams: {entName: company, pageNo: 1,pageSize:200}}
     * @return
     */
    @ResponseBody
    @PostMapping("getCreditInfoByCodes")
    public ResponseEntity getCreditInfoByCodes(@RequestParam("query") String query) {
        List<CreditQueryDTO> creditQueries = JSON.parseArray(query, CreditQueryDTO.class);

        HashMap<String, String> resultDatas = new HashMap<>();
        for (int i = 0; i < creditQueries.size(); i++) {
            CreditQueryDTO creditQuery = creditQueries.get(i);
            String code = creditQuery.getCode();
            String s = "{\"code\":\"110\",\"orderNo\":\"1187778247802515456\",\"charge\":false,\"data\":null," +
                    "\"pcode\":\"E_007_0501\",\"param\":null,\"time\":\"1703230002168\",\"message\":\"查询成功，无数据\"}";
            resultDatas.put(code, s);
        }
        return ResponseEntity.ok(resultDatas);
    }

    /**
     * 注册页面获取供应商关联表信息
     */
    @PostMapping("follow/table")
    public ResponseEntity supplierFollowTable(String query) {
        SupplierRegisterQueryDTO registerQuery = JSON.parseObject(query, SupplierRegisterQueryDTO.class);
        final RegisterBaseDTO followTableByTemplate = registerLogic.followTableByTemplate(registerQuery);
        return ResponseEntity.ok(followTableByTemplate);
    }

    /**
     * 注册页面获取供应商关联表信息
     */
    @PostMapping("follow/table/remove")
    public ResponseEntity followTableRemove(Long id, String tableType) {
        registerLogic.followTableRemove(id, tableType);
        return ResponseEntity.ok().build();
    }

    /**
     * 供应商同意协议
     */
    @PostMapping("agreement")
    public ResponseEntity agreement(String id, String version) {
        registerLogic.agreement(id, version);
        return ResponseEntity.ok(null);
    }

    /**
     * 获取校验规则
     * @param templateId 注册模板信息
     * @param type 注册界面类型
     * @return 校验规则
     */
    @RequestMapping("/getFieldValidationList")
    public List<ValidationRuleSettingInfoClientDTO> getFieldValidationList(String templateId, Integer type) {
        return registerLogic.getFieldValidationList(templateId, type);
    }
}
