package com.jy.business.contract.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jy.business.contract.model.dto.ContractInfoDto;
import com.jy.business.contract.model.dto.ContractInfoSelectDto;
import com.jy.business.contract.model.dto.OrderListDto;
import com.jy.business.contract.model.vo.ContractInfoVo;
import com.jy.business.contract.model.vo.ContractStatisticsVo;
import com.jy.business.contract.model.vo.TransactionVo;
import com.jy.business.contract.service.ContractInfoService;
import com.jy.framework.model.base.BaseSelectDto;
import com.jy.framework.model.base.R;
import com.jy.framework.model.constants.ProfileConstant;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 合同 前端控制器
 * </p>
 *
 * @author
 * @since 2025-06-10
 */
@RestController
@RequestMapping("/contractInfo")
public class ContractInfoController {

    @Resource
    private ContractInfoService contractInfoService;

    @GetMapping("/statistics")
    public List<ContractStatisticsVo> getStatistics(ContractInfoSelectDto dto) {
        return contractInfoService.getStatistics(dto);
    }

    /**
     * 合同列表
     */
    @GetMapping("/getList")
    public List<ContractInfoVo> getList(ContractInfoSelectDto dto) {
        return contractInfoService.getList(dto);
    }

    /**
     * 合同分页
     */
    @GetMapping("/getPage")
    public Page<ContractInfoVo> getPage(ContractInfoSelectDto dto) {
        return contractInfoService.getPage(dto);
    }

    /**
     * 合同明细
     */
    @GetMapping("/getDetail")
    public ContractInfoVo getDetail(BaseSelectDto dto) {
        return contractInfoService.getDetail(dto.getId());
    }

    /**
     * 合同新增
     */
    @PostMapping("/add")
    public void add(@RequestBody ContractInfoDto dto) {
        contractInfoService.add(dto);
    }

    /**
     * 合同编辑
     */
    @PostMapping("/edit")
    public void edit(@RequestBody ContractInfoDto dto) {
        contractInfoService.edit(dto);
    }

    /**
     * 合同删除
     */
    @PostMapping("/delete")
    public void delete(@RequestBody BaseSelectDto dto) {
        contractInfoService.delete(dto.getIdList());
    }

    /**
     * 交易明细
     */
    @GetMapping("getTransactionList")
    public List<TransactionVo> getTransactionList(BaseSelectDto dto) {
        return contractInfoService.getTransactionList(dto.getId());
    }


    private static final String devUrl = "http://111.229.165.209:10004/api/";
    private static final String prodUrl = "http://111.229.165.209:10002/api/";

    @GetMapping("/getOrderList")
    public Object getOrderList(OrderListDto dto) {
        dto.setSign("jy_api_request");

        String url = (ProfileConstant.PROD.equals(SpringUtil.getActiveProfile()) ? prodUrl : devUrl)
                + "api/orderInfo/getOrderList?" + HttpUtil.toParams(BeanUtil.beanToMap(dto));
        String result = HttpUtil.get(url);
        try {
            JSONObject parse = JSONObject.parse(result);
            if (parse.getInteger("code") == 200) {
                return parse.get("data");
            }
            return R.fail(parse.getString("msg"));
        } catch (Exception e) {
            return R.fail("请求失败：" + result);
        }
    }

    @PostMapping("settlement")
    public void settlement(@RequestBody ContractInfoDto dto) {
        contractInfoService.settlement(dto);
    }

}
