package com.yiqi.admin.controller.core;

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.page.TableDataInfo;
import com.yiqi.common.enums.BusinessType;
import com.yiqi.common.enums.DeleteStatus;
import com.yiqi.common.enums.SourceType;
import com.yiqi.common.utils.SecurityUtils;
import com.yiqi.common.utils.poi.ExcelUtil;
import com.yiqi.core.domain.ClothHanger;
import com.yiqi.core.domain.vo.ClothHangerVO;
import com.yiqi.core.service.IClothHangerService;
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.List;

/**
 * 挂架管理Controller
 *
 * @author ruoyi
 * @date 2023-10-05
 */
@Api(tags = "管理后台 - 挂架管理")
@RestController
@RequestMapping(UrlConstants.managerApi + "/cloth/hanger")
public class ClothHangerController extends BaseController {
    @Autowired
    private IClothHangerService clothHangerService;

    /**
     * 查询挂架管理列表
     */
    @PreAuthorize("@ss.hasPermi('cloth:hanger:list')")
    @GetMapping("/list")
    @ApiOperation(value = "查询挂架管理列表")
    public TableDataInfo list(ClothHangerVO clothHangerVO) {
        startPage();
        List<ClothHangerVO> list = clothHangerService.selectClothHangerList(clothHangerVO);
        return getDataTable(list);
    }

    /**
     * 导出挂架管理列表
     */
    @PreAuthorize("@ss.hasPermi('cloth:hanger:export')")
    @Log(title = "挂架管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出挂架管理列表")
    public void export(HttpServletResponse response, ClothHangerVO clothHangerVO) {
        List<ClothHangerVO> list = clothHangerService.selectClothHangerList(clothHangerVO);
        ExcelUtil<ClothHangerVO> util = new ExcelUtil<ClothHangerVO>(ClothHangerVO.class);
        util.exportExcel(response, list, "挂架管理数据");
    }

    /**
     * 获取挂架管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('cloth:hanger:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "获取挂架管理详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(clothHangerService.selectClothHangerById(id));
    }

    /**
     * 新增挂架管理
     */
    @Transactional(rollbackFor = Exception.class)
    @PreAuthorize("@ss.hasPermi('cloth:hanger:add')")
    @Log(title = "挂架管理", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增挂架管理")
    public AjaxResult add(@RequestBody ClothHanger clothHanger) {
        clothHanger.setCreateById(getUserId());
        clothHanger.setCreateBy(getUsername());
        return toAjax(clothHangerService.insertClothHanger(clothHanger));
    }

    /**
     * 修改挂架管理
     */
    @Transactional(rollbackFor = Exception.class)
    @PreAuthorize("@ss.hasPermi('cloth:hanger:edit')")
    @Log(title = "挂架管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改挂架管理")
    public AjaxResult edit(@RequestBody ClothHanger clothHanger) {
        clothHanger.setUpdateById(getUserId());
        clothHanger.setUpdateBy(getUsername());
        return toAjax(clothHangerService.updateClothHanger(clothHanger));
    }

    /**
     * 删除挂架管理
     */
    @PreAuthorize("@ss.hasPermi('cloth:hanger:remove')")
    @Log(title = "挂架管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "删除挂架管理")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(clothHangerService.deleteClothHangerByIds(ids));
    }

    /**
     * 修改挂架管理状态
     */
    @PreAuthorize("@ss.hasPermi('cloth:hanger:edit')")
    @Log(title = "挂架管理", businessType = BusinessType.UPDATE)
    @PostMapping(value = "updateStatus")
    @ApiOperation(value = "修改挂架管理状态")
    public AjaxResult updateClothHangerStatus(@RequestBody ClothHanger clothHanger) {
        return toAjax(clothHangerService.updateClothHangerStatus(clothHanger));
    }


    /**
     * 挂架查询 查启用的
     */
    @PreAuthorize("@ss.hasPermi('cloth:hanger:list')")
    @GetMapping("/hangerList")
    @ApiOperation(value = "挂架查询 查启用的")
    public TableDataInfo hangerList(ClothHangerVO clothHangerVO) {
        startPage();
        clothHangerVO.setSourceType(SecurityUtils.getLoginUser().getSourceType());
        clothHangerVO.setOrgId(SourceType.FACTORY.getCode().equals(
                SecurityUtils.getLoginUser().getSourceType())? SecurityUtils.getLoginUser().getUser().getFactoryId():SecurityUtils.getLoginUser().getUser().getStoreId());

        List<ClothHanger> list = clothHangerService.list(new QueryWrapper<ClothHanger>().lambda()
                .eq(ClothHanger::getDelFlag, DeleteStatus.OK.getCode())
                .eq(ClothHanger::getStatus, DeleteStatus.OK.getCode())
                .eq(ClothHanger::getSourceType, clothHangerVO.getSourceType())
                .eq(ClothHanger::getOrgId, clothHangerVO.getOrgId()));
        return getDataTable(list);
    }
}
