package cn.superdesk.uniorder.controller.setting;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.superdesk.uniorder.common.json.AnJsonUtils;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.common.rest.response.ResponseResultOuterUtils;
import cn.superdesk.uniorder.controller.base.BaseController;
import cn.superdesk.uniorder.dao.entity.base.UnifiedOrder;
import cn.superdesk.uniorder.dao.entity.setting.UnifiedService;
import cn.superdesk.uniorder.dto.param.setting.UnifiedServicePartGoodsTransportParam;
import cn.superdesk.uniorder.dto.param.setting.UnifiedServicePartOrderStateParam;
import cn.superdesk.uniorder.dto.param.setting.UnifiedServicePartPayTypeParam;
import cn.superdesk.uniorder.dto.param.setting.UnifiedServicePartServiceParam;
import cn.superdesk.uniorder.dubbo.consumer.order.base.UnifiedOrderDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.unified.UnifiedServiceDubboConsumerService;
import cn.superdesk.uniorder.common.exception.utils.ExceptionUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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


/**
 * @Description: uservice
 * @Author: linjinyu
 * @CreateDate: 2021/5/17 上午10:52
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/5/17 上午10:52
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Api(value = "系统设置-服务设置(订单来源)-20210517修改", tags = {"系统设置-服务设置(订单来源)-20210517修改"})
@Slf4j
@RestController
@RequestMapping("/unifiedService")
public class UnifiedServiceController extends BaseController {

    @Autowired
    private UnifiedServiceDubboConsumerService unifiedServiceDubboConsumerService;
    @Autowired
    private UnifiedOrderDubboConsumerService   unifiedOrderDubboConsumerService;

    @ApiOperation(value = "服务类型-下拉")
    @RequestMapping(value = "/getAsService", method = RequestMethod.POST)
    @ResponseBody
    public R<List<UnifiedService>> getAsService() {

        R<List<UnifiedService>> result = new R<>();

        result.setData(unifiedServiceDubboConsumerService.getAsService(getToken()));

        return result;
    }

    @ApiOperation(value = "订单类型-下拉")
    @RequestMapping(value = "/getAsOrderType", method = RequestMethod.POST)
    @ResponseBody
    public R<List<UnifiedService>> getAsOrderType() {

        R<List<UnifiedService>> result = new R<>();

        result.setData(unifiedServiceDubboConsumerService.getAsOrderType(getToken()));

        return result;
    }


    @ApiOperation(value = "服务列表分页")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    @ResponseBody
    public R<List<UnifiedService>> getdata() {

        R<List<UnifiedService>> result = new R<>();

        try {

            Map<String, Object> params = new HashMap<>();

            params.put("serviceName", getString("serviceName"));
            params.put("orderType", getString("orderType"));

            result = unifiedServiceDubboConsumerService.getPage(getToken(), params, getPageNum(), getPageSize());

        } catch (Exception e) {

            result.setCode(String.valueOf(e.hashCode()));
            result.setSuccess(false);
            result.setMessage(e.getMessage());

            log.error(ExceptionUtils.getMessage(e));
            e.printStackTrace();
        }

        return result;
    }

    @ApiOperation(value = "新增服务")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public R<UnifiedService> add(UnifiedService entity) {

        R<UnifiedService> result = new R<>();

        try {

            UnifiedService service = unifiedServiceDubboConsumerService.addObj(getToken(), entity);
            result.setSuccess(true);
            result.setData(service);

        } catch (Exception e) {

            result.setCode(String.valueOf(e.hashCode()));
            result.setSuccess(false);
            result.setMessage(e.getMessage());

            log.error(ExceptionUtils.getMessage(e));
            e.printStackTrace();

        }

        return result;

    }

    @ApiOperation(value = "修改服务 All")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public R<UnifiedService> update(UnifiedService entity) {

        R<UnifiedService> result = new R<>();

        try {

            if (entity.getId() == null) {

                result.setCode(400);
                result.setSuccess(false);
                result.setMessage("请选择修改数据！");
                return result;
            }

            UnifiedService checkEntity = unifiedServiceDubboConsumerService.findById(entity.getId());

            if (!checkEntity.getServiceNo().equals(entity.getServiceNo())) {

                Map<String, Object> params = new HashMap<>();
                params.put("serviceNo", entity.getServiceNo());

                List<UnifiedOrder> uos = unifiedOrderDubboConsumerService.findAll(params, getToken());
                if (uos != null && uos.size() > 0) {
                    result.setCode(400);
                    result.setSuccess(false);
                    result.setMessage("服务号已经使用,不可随意更改！");
                    return result;
                }
            }

            UnifiedService unifiedServiceEntity = unifiedServiceDubboConsumerService.updateObj(getToken(), entity);
            result.setSuccess(true);
            result.setData(unifiedServiceEntity);

        } catch (Exception e) {

            result.setCode(String.valueOf(e.hashCode()));
            result.setSuccess(false);
            result.setMessage(e.getMessage());

            log.error(ExceptionUtils.getMessage(e));
            e.printStackTrace();
        }

        return result;
    }

    @ApiOperation(value = "修改服务-part-订单-运费税率默认")
    @RequestMapping(value = "/updateUnifiedServicePartGoodsTransport", method = RequestMethod.POST)
    @ResponseBody
    public R<UnifiedService> updateUnifiedServicePartGoodsTransport(@RequestBody UnifiedServicePartGoodsTransportParam partGoodsTransportParam) {

        R<UnifiedService> result = new R<>();

        try {

            UnifiedService service = unifiedServiceDubboConsumerService.updateUnifiedServicePartGoodsTransport(getToken(), partGoodsTransportParam);
            result.setSuccess(true);
            result.setData(service);

        } catch (Exception e) {

            result.setCode(String.valueOf(e.hashCode()));
            result.setSuccess(false);
            result.setMessage(e.getMessage());

            log.error(ExceptionUtils.getMessage(e));
            e.printStackTrace();

        }

        return result;
    }

    @ApiOperation(value = "修改服务-part-订单-订单状态")
    @RequestMapping(value = "/updateUnifiedServicePartOrderState", method = RequestMethod.POST)
    @ResponseBody
    public R<UnifiedService> updateUnifiedServicePartOrderState(@RequestBody UnifiedServicePartOrderStateParam params) {


        log.info("修改服务-part-订单-订单状态 {}", AnJsonUtils.toJson(params));
        R<UnifiedService> result = new R<>();

        try {

            UnifiedService unifiedService = unifiedServiceDubboConsumerService.updateUnifiedServicePartOrderState(getToken(), params);
            result.setSuccess(true);
            result.setData(unifiedService);

        } catch (Exception e) {

            result.setCode(String.valueOf(e.hashCode()));
            result.setSuccess(false);
            result.setMessage(e.getMessage());

            log.error(ExceptionUtils.getMessage(e));
            e.printStackTrace();

        }

        return result;
    }

    @ApiOperation(value = "修改服务-part-订单-支付方式")
    @RequestMapping(value = "/updateUnifiedServicePartPayType", method = RequestMethod.POST)
    @ResponseBody
    public R<UnifiedService> updateUnifiedServicePartPayType(@RequestBody UnifiedServicePartPayTypeParam params) {

        R<UnifiedService> result = new R<>();

        try {

            UnifiedService service = unifiedServiceDubboConsumerService.updateUnifiedServicePartPayType(getToken(), params);
            result.setSuccess(true);
            result.setData(service);

        } catch (Exception e) {

            result.setCode(String.valueOf(e.hashCode()));
            result.setSuccess(false);
            result.setMessage(e.getMessage());

            log.error(ExceptionUtils.getMessage(e));
            e.printStackTrace();

        }

        return result;
    }

    @ApiOperation(value = "修改服务-part-服务部分")
    @RequestMapping(value = "/updateUnifiedServicePartService", method = RequestMethod.POST)
    @ResponseBody
    public R<UnifiedService> updateUnifiedServicePartService(@RequestBody UnifiedServicePartServiceParam partServiceParam) {

        R<UnifiedService> result = new R<>();

        try {

            UnifiedService service = unifiedServiceDubboConsumerService.updateUnifiedServicePartService(getToken(), partServiceParam);
            result.setSuccess(true);
            result.setData(service);

        } catch (Exception e) {

            result.setCode(String.valueOf(e.hashCode()));
            result.setSuccess(false);
            result.setMessage(e.getMessage());

            log.error(ExceptionUtils.getMessage(e));
            e.printStackTrace();

        }

        return result;
    }

    @ApiOperation(value = "删除服务")
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @ResponseBody
    public R<UnifiedService> delete(Long id) {

        R<UnifiedService> result = new R<>();

        try {
            UnifiedService unifiedServiceEntity = unifiedServiceDubboConsumerService.findById(id);

            if (ObjectUtil.isNull(unifiedServiceEntity)) {
                return ResponseResultOuterUtils.createErrorResult("500", "删除服务失败 [未找到] ");
            }

            if (StrUtil.isNotBlank(unifiedServiceEntity.getServiceNo())) {

                Map<String, Object> params = new HashMap<>();
                params.put("serviceNo", unifiedServiceEntity.getServiceNo());

//			List<UnifiedOrder> uos = unifiedOrderDubboConsumerService.findAll(getToken(), params);
                Integer rSize = unifiedOrderDubboConsumerService.countAll(params);

                if (rSize != null && rSize.compareTo(0) > 0) {
                    result.setCode(400);
                    result.setSuccess(false);
                    result.setMessage("服务号已经使用,不可删除！");
                    return result;
                }
            }


            unifiedServiceDubboConsumerService.delObj(id);

            result.setSuccess(true);

        } catch (Exception e) {

            result.setCode(String.valueOf(e.hashCode()));
            result.setSuccess(false);
            result.setMessage(e.getMessage());

            log.error(ExceptionUtils.getMessage(e));
            e.printStackTrace();
        }

        return result;
    }


    /**
     * 弃用 分成作为服务还是作为订单类型
     *
     * @param code
     * @return
     */
    @Deprecated
    @ApiOperation(value = "订单类型获取状态列表")
    @RequestMapping(value = "/getstatus", method = RequestMethod.POST)
    @ResponseBody
    public R<String[]> getOrderStatus(String code) {

        R<String[]> result = new R<>();

        try {

            UnifiedService unifiedServiceEntity = unifiedServiceDubboConsumerService.findByCode(code);

            if (unifiedServiceEntity.getOrderState() == null) {
                result.setData(new String[]{});
            } else {
                String[] strs = unifiedServiceEntity.getOrderState().split(",");
                result.setData(strs);
            }

        } catch (Exception e) {

            result.setCode(String.valueOf(e.hashCode()));
            result.setSuccess(false);
            result.setMessage(e.getMessage());

            log.error(ExceptionUtils.getMessage(e));
            e.printStackTrace();
        }

        return result;
    }

}
