package com.yc.ship.module.ota.controller.admin.distributorproduct;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yc.ship.framework.common.pojo.CommonResult;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.framework.operatelog.core.annotations.OperateLog;
import com.yc.ship.framework.tenant.core.context.TenantContextHolder;
import com.yc.ship.module.ota.controller.admin.distributor.vo.DistributorRespVO;
import com.yc.ship.module.ota.controller.admin.distributorproduct.vo.*;
import com.yc.ship.module.ota.controller.admin.distributorproduct.vo.product.InitializeDistributorProductReqVO;
import com.yc.ship.module.ota.controller.admin.distributorproduct.vo.product.ProductGroupRespVO;
import com.yc.ship.module.ota.controller.admin.distributorproduct.vo.product.ProductPageReqVO;
import com.yc.ship.module.ota.controller.admin.distributorproduct.vo.product.ProductRespVO;
import com.yc.ship.module.ota.dal.dataobject.distributorproduct.DistributorAgencyPriceDO;
import com.yc.ship.module.ota.dal.dataobject.distributorproduct.DistributorProductDO;
import com.yc.ship.module.ota.dal.mysql.distributor.DistributorMapper;
import com.yc.ship.module.ota.service.distributor.DistributorService;
import com.yc.ship.module.ota.service.distributorproduct.DistributorProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static com.yc.ship.framework.common.pojo.CommonResult.success;
import static com.yc.ship.framework.operatelog.core.enums.OperateTypeEnum.OTHER;

@Tag(name = "管理后台 - 平台产品与分销商关系")
@RestController
@RequestMapping("/distributor-product")
@Validated
public class DistributorProductController {

    @Resource
    private DistributorProductService distributorProductService;

    @Resource
    private DistributorService distributorService;

    @PostMapping("/create")
    @Operation(summary = "创建平台产品与分销商关系")
    @PreAuthorize("@ss.hasPermission('ota:distributor-product:create')")
    public CommonResult<String> createDistributorProduct(@Valid @RequestBody DistributorProductSaveReqVO createReqVO) {
        return success(distributorProductService.createDistributorProduct(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新平台产品与分销商关系")
    @PreAuthorize("@ss.hasPermission('ota:distributor-product:update')")
    public CommonResult<Boolean> updateDistributorProduct(@Valid @RequestBody DistributorProductSaveReqVO updateReqVO) {
        distributorProductService.updateDistributorProduct(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除平台产品与分销商关系")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ota:distributor-product:delete')")
    public CommonResult<Boolean> deleteDistributorProduct(@RequestParam("id") String id) {
        distributorProductService.deleteDistributorProduct(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得平台产品与分销商关系")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('ota:distributor-product:query')")
    public CommonResult<DistributorProductRespVO> getDistributorProduct(@RequestParam("id") String id) {
        DistributorProductDO distributorProduct = distributorProductService.getDistributorProduct(id);
        return success(BeanUtils.toBean(distributorProduct, DistributorProductRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得平台产品与分销商关系分页")
//    @PreAuthorize("@ss.hasPermission('ota:distributor-product:query')")
    public CommonResult<PageResult<DistributorProductRespVO>> getDistributorProductPage(@Valid DistributorProductPageReqVO pageReqVO) {
        IPage<DistributorProductRespVO> pageResult = distributorProductService.getDistributorProductPage(pageReqVO);
        return success(new PageResult<>(pageResult.getRecords(),pageResult.getTotal()));
    }

    @GetMapping("/pageDistributorProduct")
    @Operation(summary = "获得分销商分销产品分页")
//    @PreAuthorize("@ss.hasPermission('ota:distributor-product:query')")
    public CommonResult<PageResult<DistributorProductRespVO>> pageDistributorProduct(@Valid DistributorProductPageReqVO pageReqVO) {
        ProductPageReqVO vo = new ProductPageReqVO();
        //查询分销商信息
        DistributorRespVO DistributorVo = distributorService.getADistributorById(pageReqVO.getDistributorId());
        if(DistributorVo != null){
            vo.setChannelType(DistributorVo.getType());
            vo.setDistributorId(DistributorVo.getId());
            vo.setProductName(pageReqVO.getProductName());
            vo.setPageNo(pageReqVO.getPageNo());
            vo.setPageSize(pageReqVO.getPageSize());
        }
        IPage<DistributorProductRespVO> pageResult = distributorProductService.pageDistributorProduct(vo);
        return success(new PageResult<>(pageResult.getRecords(),pageResult.getTotal()));
    }

    @GetMapping("/distributorProductTree")
    @Operation(summary = "获得产品树及分销商分销产品")
    public CommonResult<List<DistributorProductRespVO>> distributorProductTree(@Valid ProductPageReqVO pageReqVO) {
        //查询分销商信息
        DistributorRespVO DistributorVo = distributorService.getADistributorById(pageReqVO.getDistributorId());
        if(DistributorVo != null){
            pageReqVO.setChannelType(DistributorVo.getType());
            pageReqVO.setDistributorId(DistributorVo.getId());
        }
        return success(distributorProductService.distributorProductTree(pageReqVO));
    }

    @PutMapping("/update-status")
    @Operation(summary = "更改状态")
    @PreAuthorize("@ss.hasPermission('ota:distributor-product:update')")
    public CommonResult<Boolean> updateStatus(@Valid @RequestBody DistributorProductSaveReqVO updateReqVO){
        distributorProductService.updateStatus(updateReqVO.getId(),updateReqVO.getIsUse());
        return success(true);
    }

    @GetMapping("/productPage")
    @Operation(summary = "获得分销产品分页")
//    @PreAuthorize("@ss.hasPermission('ota:distributor-product:query')")
    public CommonResult<PageResult<ProductRespVO>> getProductPage(@Valid ProductPageReqVO pageReqVO) {
        IPage<ProductRespVO> pageResult = distributorProductService.getProductPage(pageReqVO);
        return success(new PageResult<>(pageResult.getRecords(),pageResult.getTotal()));
    }

    @GetMapping("/productTree")
    @Operation(summary = "获得产品树")
    public CommonResult<List<ProductRespVO>> productTree(@Valid ProductPageReqVO pageReqVO) {
        return success(distributorProductService.productTree(pageReqVO));
    }

    @PostMapping("/initializeProductDistributorInfo")
    @Operation(summary = "初始化当前分销商与产品关联关系")
//    @PreAuthorize("@ss.hasPermission('ota:distributor-product:query')")
    public CommonResult<Boolean> initializeProductDistributorInfo(@Valid DistributorProductPageReqVO reqVO) {
        distributorProductService.initializeProductDistributorInfo(reqVO);
        return success(true);
    }

    @PostMapping("/initializeDistributorProductInfo")
    @Operation(summary = "初始化当前产品与分销商关联关系")
//    @PreAuthorize("@ss.hasPermission('ota:distributor-product:query')")
    public CommonResult<Boolean> initializeDistributorProductInfo(@Valid InitializeDistributorProductReqVO reqVO) {
        distributorProductService.initializeDistributorProductInfo(reqVO);
        return success(true);
    }

    @PostMapping("/batchBinding")
    @Operation(summary = "批量绑定产品")
    public CommonResult<Boolean> BatchBinding(@Valid @RequestBody List<InitializeDistributorProductReqVO> reqVOs) {
        if(!reqVOs.isEmpty()){
            for (InitializeDistributorProductReqVO reqVO : reqVOs){
                distributorProductService.initializeDistributorProductInfo(reqVO);
            }
        }
        return success(true);
    }

    @PutMapping("/update-status-otc")
    @Operation(summary = "更改状态")
    public CommonResult<Boolean> updateStatusOtc(@Valid @RequestBody DistributorProductSaveReqVO updateReqVO) {
        distributorProductService.updateStatusOtc(updateReqVO.getId(),updateReqVO.getIsUse());
        return success(true);
    }

    @PutMapping("/update-status-list")
    @Operation(summary = "批量更改状态")
    public CommonResult<Boolean> updateStatusList(@Valid @RequestBody DistributorProductSaveReqVO updateReqVO) {
        if (updateReqVO.getDistributorIds() != null) {
            distributorProductService.updateStatus2List(updateReqVO);
            if (updateReqVO.getSyncToStore() != null && updateReqVO.getSyncToStore() == 1) {
                distributorProductService.updateStoreProductList(updateReqVO);
            }
        } else {
            distributorProductService.updateStatusList(updateReqVO);
        }
        return success(true);
    }

    @PutMapping("/update-statusInit-list")
    @Operation(summary = "批量更改状态和初始化")
    public CommonResult<Boolean> updateStatusInitList(@Valid @RequestBody List<DistributorProductSaveReqVO> updateReqVO) {
        distributorProductService.updateStatusInitList(updateReqVO);
        return success(true);
    }

    @GetMapping("/selectProjectType")
    @Operation(summary = "通过产品ID查询所有分销商基础票规格信息")
    public CommonResult<List<ProductGroupRespVO>> selectProjectType(@Valid Long productId) {
        List<ProductGroupRespVO> projectType = distributorProductService.selectProjectType(productId);
        return success(BeanUtils.toBean(projectType, ProductGroupRespVO.class));
    }

    @PostMapping("/selectAgencyPrice")
    @Operation(summary = "通过旅行社和产品ID查询所有规格信息")
    public CommonResult<List<DistributorAgencyPriceRespVO>> selectAgencyPrice(@Valid DistributorAgencyPriceDO vo) {
        List<DistributorAgencyPriceDO> projectType;
        if (vo.getUseTenant() == 0) {
            TenantContextHolder.setIgnore(true);
            projectType = distributorProductService.selectAgencyPrice(vo);
            TenantContextHolder.setIgnore(false);
        } else {
            projectType = distributorProductService.selectAgencyPrice(vo);
        }
        return success(BeanUtils.toBean(projectType, DistributorAgencyPriceRespVO.class));
    }

    @PostMapping("/updateProjectType")
    @Operation(summary = "修改OTA分销商基础票规格信息")
    public CommonResult<Boolean> updateProjectType(@Valid @RequestBody DistributorProjectSaveReqVO createReqVO) {
        distributorProductService.updateProjectType(createReqVO);
        return success(true);
    }

    @PostMapping("/updateAgencyPrice")
    @Operation(summary = "修改旅行社规格金额信息")
    public CommonResult<Boolean> updateAgencyPrice(@Valid @RequestBody DistributorAgencyPriceDO createReqVO) {
        distributorProductService.updateAgencyPrice(createReqVO);
        return success(true);
    }

    @PostMapping("/syncTicketSave")
    @Operation(summary = "抖音日历票同步产品")
    @OperateLog(type = OTHER)
    public CommonResult<?> syncTicketSave(@Valid @RequestBody List<DistributorProductSaveReqVO> distributorProductList) {

        return distributorProductService.syncTicketSave(distributorProductList);
    }

    @PutMapping("/relationOtaInfo")
    @Operation(summary = "关联OTA信息")
    public CommonResult<Boolean> relationOtaInfo(@Valid @RequestBody DistributorProductSaveReqVO updateReqVO) {
        if (updateReqVO.getIdList() != null) {
            distributorProductService.relationOtaInfo(updateReqVO);
        }
        return success(true);
    }

    @PostMapping("/syncTicketQuery")
    @Operation(summary = "抖音日历票同步审核状态")
    @OperateLog(type = OTHER)
    public CommonResult<?> syncTicketQuery(@Valid @RequestBody List<DistributorProductSaveReqVO> distributorProductList) {

        return distributorProductService.syncTicketQuery(distributorProductList);
    }

    @PostMapping("/syncTicketCalendarSave")
    @Operation(summary = "抖音日历票同步价格库存")
    @OperateLog(type = OTHER)
    public CommonResult<?> syncTicketCalendarSave(@Valid @RequestBody List<DistributorProductSaveReqVO> distributorProductList) {
        return distributorProductService.syncTicketCalendarSave(distributorProductList);
    }

    @PostMapping("/syncProductOperate")
    @Operation(summary = "抖音日历票同步上下架状态")
    @OperateLog(type = OTHER)
    public CommonResult<?> syncProductOperate(@Valid @RequestBody List<DistributorProductSaveReqVO> distributorProductList) {
        return distributorProductService.syncProductOperate(distributorProductList);
    }
}
