package com.yc.ship.module.product.controller.admin.distributorDiscount;

import com.yc.ship.framework.apilog.core.annotation.ApiAccessLog;
import com.yc.ship.framework.common.pojo.CommonResult;
import com.yc.ship.framework.common.pojo.PageParam;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.collection.CollectionUtils;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.framework.excel.core.util.ExcelUtils;
import com.yc.ship.module.product.controller.admin.distributorDiscount.vo.DistributorDiscountCountryRespVO;
import com.yc.ship.module.product.controller.admin.distributorDiscount.vo.DistributorDiscountPageReqVO;
import com.yc.ship.module.product.controller.admin.distributorDiscount.vo.DistributorDiscountRespVO;
import com.yc.ship.module.product.controller.admin.distributorDiscount.vo.DistributorDiscountSaveReqVO;
import com.yc.ship.module.product.dal.dataobject.distributorDiscount.DistributorDiscountDO;
import com.yc.ship.module.product.service.distributorDiscount.DistributorDiscountService;
import com.yc.ship.module.system.api.area.AreaApi;
import com.yc.ship.module.system.api.area.dto.AreaCountryDTO;
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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static com.yc.ship.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.yc.ship.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 分销商专属折扣")
@RestController
@RequestMapping("/product/distributor-discount")
@Validated
public class DistributorDiscountController {

    @Resource
    private DistributorDiscountService distributorDiscountService;

    @Resource
    private AreaApi areaApi;


    @PostMapping("/create")
    @Operation(summary = "创建分销商专属折扣")
    public CommonResult<Long> createDistributorDiscount(@Valid @RequestBody DistributorDiscountSaveReqVO createReqVO) {
        return success(distributorDiscountService.createDistributorDiscount(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新分销商专属折扣")
    public CommonResult<Boolean> updateDistributorDiscount(@Valid @RequestBody DistributorDiscountSaveReqVO updateReqVO) {
        distributorDiscountService.updateDistributorDiscount(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除分销商专属折扣")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteDistributorDiscount(@RequestParam("id") Long id) {
        distributorDiscountService.deleteDistributorDiscount(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得分销商专属折扣")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<DistributorDiscountRespVO> getDistributorDiscount(@RequestParam("id") Long id) {
        DistributorDiscountDO distributorDiscount = distributorDiscountService.getDistributorDiscount(id);
        DistributorDiscountRespVO bean = BeanUtils.toBean(distributorDiscount, DistributorDiscountRespVO.class);
        return success(bean);
    }

    @GetMapping("/getByTravlId")
    @Operation(summary = "获得分销商专属折扣")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<List<DistributorDiscountCountryRespVO>> getByTravlId(@RequestParam("id") Long id, @RequestParam("shipId") String shipId) {
        DistributorDiscountDO distributorDiscount = distributorDiscountService.getByTravlId(id,shipId);
        if(distributorDiscount==null){
            return success(null);
        }
        DistributorDiscountRespVO bean = BeanUtils.toBean(distributorDiscount, DistributorDiscountRespVO.class);
        List<Long> areaId = bean.getAreaId();
        if(CollectionUtils.isAnyEmpty(areaId)) {
            return success(null);
        }
        List<AreaCountryDTO> areaCountry = areaApi.getAreaCountryListByAreaIds(areaId);
        List<DistributorDiscountCountryRespVO> result = BeanUtils.toBean(areaCountry, DistributorDiscountCountryRespVO.class);
        result.forEach(item->{
            item.setId(distributorDiscount.getId());
            item.setDiscountNum(distributorDiscount.getDiscountNum());
        });
        return success(result);
    }

    @GetMapping("/page")
    @Operation(summary = "获得分销商专属折扣分页")
    public CommonResult<PageResult<DistributorDiscountRespVO>> getDistributorDiscountPage(@Valid DistributorDiscountPageReqVO pageReqVO) {
        PageResult<DistributorDiscountDO> pageResult = distributorDiscountService.getDistributorDiscountPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DistributorDiscountRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出分销商专属折扣 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDistributorDiscountExcel(@Valid DistributorDiscountPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DistributorDiscountDO> list = distributorDiscountService.getDistributorDiscountPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "分销商专属折扣.xls", "数据", DistributorDiscountRespVO.class,
                        BeanUtils.toBean(list, DistributorDiscountRespVO.class));
    }

}