package com.yiqi.admin.controller.core;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yiqi.common.annotation.Log;
import com.yiqi.common.constant.UrlConstants;
import com.yiqi.common.core.controller.BaseController;
import com.yiqi.common.core.domain.AjaxResult;
import com.yiqi.common.core.domain.R;
import com.yiqi.common.core.page.TableDataInfo;
import com.yiqi.common.enums.BusinessType;
import com.yiqi.common.utils.poi.ExcelUtil;
import com.yiqi.core.domain.ClothHangerDetail;
import com.yiqi.core.domain.vo.ClothHangerDetailVO;
import com.yiqi.core.domain.vo.ClothHangerOrderRelationVO;
import com.yiqi.core.service.IClothHangerDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 挂架位置详情Controller
 *
 * @author ruoyi
 * @date 2023-10-06
 */
@Api(tags = "管理后台 - 挂架位置详情")
@RestController
@RequestMapping(UrlConstants.managerApi + "/cloth/detail")
public class ClothHangerDetailController extends BaseController {
    @Autowired
    private IClothHangerDetailService clothHangerDetailService;

    /**
     * 查询挂架位置详情列表
     */
    @PreAuthorize("@ss.hasPermi('cloth:hanger:list')")
    @GetMapping("/list")
    @ApiOperation(value = "查询挂架位置详情列表")
    public TableDataInfo list(ClothHangerDetail clothHangerDetail) {
        startPage();
        List<ClothHangerDetail> list = clothHangerDetailService.selectClothHangerDetailList(clothHangerDetail);
        return getDataTable(list);
    }

    /**
     * 查询挂架位置详情列表
     */
    @GetMapping("/getIsHangerList")
    @ApiOperation(value = "查询挂架位置详情列表")
    public R<List<ClothHangerDetailVO>> getIsHangerList(@RequestParam("hangerId") Long hangerId) {
        List<ClothHangerDetailVO> list = clothHangerDetailService.getIsHangerList(hangerId);
        return R.ok(list);
    }

    /**
     * 导出挂架位置详情列表
     */
//    @PreAuthorize("@ss.hasPermi('cloth:detail:export')")
    @Log(title = "挂架位置详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出挂架位置详情列表")
    public void export(HttpServletResponse response, ClothHangerDetail clothHangerDetail) {
        List<ClothHangerDetail> list = clothHangerDetailService.selectClothHangerDetailList(clothHangerDetail);
        ExcelUtil<ClothHangerDetail> util = new ExcelUtil<ClothHangerDetail>(ClothHangerDetail.class);
        util.exportExcel(response, list, "挂架位置详情数据");
    }

    /**
     * 获取挂架位置详情详细信息
     */
//    @PreAuthorize("@ss.hasPermi('cloth:detail:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "获取挂架位置详情详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(clothHangerDetailService.selectClothHangerDetailById(id));
    }

    /**
     * 新增挂架位置详情
     */
//    @PreAuthorize("@ss.hasPermi('cloth:detail:add')")
    @Log(title = "挂架位置详情", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增挂架位置详情")
    public AjaxResult add(@RequestBody ClothHangerDetail clothHangerDetail) {
        clothHangerDetail.setCreateById(getUserId());
        clothHangerDetail.setCreateBy(getUsername());
        return toAjax(clothHangerDetailService.insertClothHangerDetail(clothHangerDetail));
    }

    /**
     * 修改挂架位置详情
     */
//    @PreAuthorize("@ss.hasPermi('cloth:detail:edit')")
    @Log(title = "挂架位置详情", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改挂架位置详情")
    public AjaxResult edit(@RequestBody ClothHangerDetail clothHangerDetail) {
        clothHangerDetail.setUpdateById(getUserId());
        clothHangerDetail.setUpdateBy(getUsername());
        return toAjax(clothHangerDetailService.updateClothHangerDetail(clothHangerDetail));
    }

    /**
     * 删除挂架位置详情
     */
//    @PreAuthorize("@ss.hasPermi('cloth:detail:remove')")
    @Log(title = "挂架位置详情", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "删除挂架位置详情")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(clothHangerDetailService.deleteClothHangerDetailByIds(ids));
    }

    /**
     * 获取挂架位置已经上架的订单衣物详细信息
     */
    @GetMapping(value = "/getClothOrderInfoByHangerCodeAndId")
    @ApiOperation(value = "获取挂架位置详情详细信息")
    public R<List<ClothHangerOrderRelationVO>> getClothOrderInfoByHangerCodeAndId(@RequestParam("hangerId") Long hangerId, @RequestParam("hangerCode") String hangerCode) {
        List<ClothHangerOrderRelationVO> list = clothHangerDetailService.getClothOrderInfoByHangerCodeAndId(hangerId, hangerCode);
        if (CollUtil.isEmpty(list)){
            return R.ok(new ArrayList<>());
        }
        return R.ok(list);
    }

    /**
     * 通过衣架id和code查询位置信息
     */
    @GetMapping(value = "/getHangerInfoByHangerCodeAndId")
    @ApiOperation(value = "获取挂架位置详情详细信息")
    public R<ClothHangerDetail> getHangerInfoByHangerCodeAndId(@RequestParam("hangerId") Long hangerId, @RequestParam("hangerCode") String hangerCode) {
        return R.ok(clothHangerDetailService.getOne(new QueryWrapper<ClothHangerDetail>().lambda()
                .eq(ClothHangerDetail::getHangerId, hangerId)
                .eq(ClothHangerDetail::getCode, hangerCode)));
    }

    /**
     * 上挂
     */
    @Transactional(rollbackFor = Exception.class)
    @GetMapping(value = "/upHanger")
    @ApiOperation(value = "上挂")
    public R<Integer> upHanger(@RequestParam("orderClothItemId") Long orderClothItemId,
                               @RequestParam("hangerId") Long hangerId,
                               @RequestParam("hangerCode") String hangerCode) {
        return R.ok(clothHangerDetailService.upHanger(orderClothItemId, hangerId, hangerCode));
    }
}
