package com.crm.custom.controller;

import com.crm.custom.service.GoldenOrderService;
import com.crm.rely.backend.core.constant.FeignClientAnnotation;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.dto.base.ResultWithPagerDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.model.dto.golden.order.GoldenOrderListDto;
import com.crm.rely.backend.model.entity.custom.info.CustomInfoEntity;
import com.crm.rely.backend.model.entity.golden.order.GoldenOrderAddEntity;
import com.crm.rely.backend.model.entity.golden.order.GoldenOrderSearchEntity;
import com.crm.rely.backend.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/order")
public class GoldenOrderController {

    @Autowired
    private GoldenOrderService goldenOrderService;

    @PostMapping("/search/list")
    public ResultWithPagerDto<GoldenOrderListDto> customPageList(@RequestBody GoldenOrderSearchEntity entity, CustomInfoEntity infoEntity) throws Exception {
        if (infoEntity == null || infoEntity.getId() == null) {
            entity.setCustomId(-1l);
        }else {
            entity.setCustomId(infoEntity.getId());
        }
        if (entity.getEndDate() != null) {
            entity.setEndDate(DateUtil.addDate(entity.getEndDate(), 1));
        }
        return goldenOrderService.searchPageList(entity);
    }

    @PostMapping("/cancel")
    public BaseResultDto cancelOrder(@RequestBody @Validated SingleLongEntity entity) throws Exception {
        goldenOrderService.cancelOrder(entity.getId());
        return BaseResultDto.success();
    }


    @PostMapping("/generated")
    public BaseResultDto generated(@RequestBody @Validated GoldenOrderAddEntity entity, CustomInfoEntity infoEntity) throws Exception {
        goldenOrderService.generated(entity, infoEntity);
        return BaseResultDto.success();
    }

    @PostMapping("/manager/search/list")
    @FeignClientAnnotation
    public ResultWithPagerDto<GoldenOrderListDto> pageList(@RequestBody GoldenOrderSearchEntity entity) throws Exception {
        return goldenOrderService.searchPageList(entity);
    }

    @PostMapping("/manager/complete")
    @FeignClientAnnotation
    public BaseResultDto completeOrder(@RequestBody @Validated SingleLongEntity entity) throws Exception {
        goldenOrderService.completeOrder(entity.getId());
        return BaseResultDto.success();
    }

    @PostMapping("/manager/cancel")
    @FeignClientAnnotation
    public BaseResultDto managerCancelOrder(@RequestBody @Validated SingleLongEntity entity) throws Exception {
        //goldenOrderService.cancelOrder(entity.getId());
        return BaseResultDto.success();
    }

}
