package com.ruoyi.web.controller.mall;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.page.RespPage;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mall.domain.Product;
import com.ruoyi.mall.domain.query.ProductQuery;
import com.ruoyi.mall.domain.vo.ProductVO;
import com.ruoyi.mall.service.ProductService;
import com.ruoyi.school.course.domain.req.course.ReqSearchScCourse;
import com.ruoyi.school.course.domain.resp.course.RespSearchCourse;
import com.ruoyi.school.course.domain.ScCourse;
import com.ruoyi.school.course.service.IScCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品信息Controller
 *
 * @author zcc
 * @date 2022-11-28
 */
@Api(description ="商品信息接口列表")
@RestController
@RequestMapping("/pms/product")
public class ProductController extends BaseController {
    @Autowired
    private ProductService service;
    @Autowired
    private IScCourseService courseService;

    @ApiOperation("查询商品信息列表")
    @PreAuthorize("@ss.hasPermi('pms:product:list')")
    @PostMapping("/list")
    public ResponseEntity<Page<Product>> list(@RequestBody ProductQuery query, Pageable page) {
//        service.getIds(query);
        List<Product> list = service.selectList(query, page);
        return ResponseEntity.ok(new PageImpl<>(list, page, ((com.github.pagehelper.Page)list).getTotal()));
    }

    @ApiOperation("导出商品信息列表")
    @PreAuthorize("@ss.hasPermi('pms:product:export')")
    @Log(title = "商品信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public ResponseEntity<String> export(ProductQuery query) {
//        List<Product> list = service.selectList(query, null);
//        ExcelUtil<ProductVO> util = new ExcelUtil<>(ProductVO.class);
//        return ResponseEntity.ok(util.writeExcel(convert.dos2vos(list), "商品信息数据"));
        return null;
    }

    @ApiOperation("获取商品信息详细信息")
    @PreAuthorize("@ss.hasPermi('pms:product:query')")
    @GetMapping(value = "/{id}")
    public ResponseEntity<ProductVO> getInfo(@PathVariable("id") Long id) {
        ProductVO productVO= service.selectById(id);
        if (productVO.getIsCourse()==1)
        {
            ScCourse course= courseService.getById(productVO.getCourseId());

            productVO.setCourseName(ObjectUtil.isNotEmpty(course)?course.getCourseName():"**课程不存在**");
        }
        return ResponseEntity.ok(productVO);
    }

    @ApiOperation("新增商品信息")
    @PreAuthorize("@ss.hasPermi('pms:product:add')")
    @Log(title = "商品信息", businessType = BusinessType.INSERT)
    @PostMapping
    public ResponseEntity<Integer> add(@RequestBody ProductVO product) {
        return ResponseEntity.ok(service.insert(product));
    }

    @ApiOperation("修改商品信息")
    @PreAuthorize("@ss.hasPermi('pms:product:edit')")
    @Log(title = "商品信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResponseEntity<Integer> edit(@RequestBody ProductVO product) {
        return ResponseEntity.ok(service.update(product));
    }

    @ApiOperation("删除商品信息")
    @PreAuthorize("@ss.hasPermi('pms:product:remove')")
    @Log(title = "商品信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public ResponseEntity<Integer> remove(@PathVariable Long id) {
        return ResponseEntity.ok(service.deleteById(id));
    }

    @PostMapping("/courseList")
    public ResponseEntity<RespPage<RespSearchCourse>> listCourseForProduct(@RequestBody ReqSearchScCourse query){
        return ResponseEntity.ok(courseService.searchCourse(query));
    }
}
