package com.yiqi.admin.controller.core;

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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yiqi.common.core.domain.R;
import com.yiqi.common.enums.DeleteStatus;
import com.yiqi.common.enums.SourceType;
import com.yiqi.common.enums.StatusType;
import com.yiqi.core.domain.vo.ClothItemVo;
import com.yiqi.core.domain.vo.ClothItemWithPriceQueryVO;
import com.yiqi.core.domain.vo.ClothPriceSetVO;
import com.yiqi.system.domain.SysOrg;
import com.yiqi.system.domain.SysStore;
import com.yiqi.system.service.ISysOrgService;
import com.yiqi.system.service.ISysStoreService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.yiqi.common.annotation.Log;
import com.yiqi.common.core.controller.BaseController;
import com.yiqi.common.core.domain.AjaxResult;
import com.yiqi.common.enums.BusinessType;
import com.yiqi.core.domain.ClothItem;
import com.yiqi.core.service.IClothItemService;
import com.yiqi.common.utils.poi.ExcelUtil;
import com.yiqi.common.core.page.TableDataInfo;

/**
 * 衣服Controller
 *
 * @author ruoyi
 * @date 2023-08-10
 */
@Api(tags = "管理后台 - 衣服")
@RestController
@RequestMapping("/mapi/core/clothItem")
public class ClothItemController extends BaseController {
    @Autowired
    private IClothItemService clothItemService;
    @Autowired
    private ISysStoreService sysStoreService;
    @Autowired
    private ISysOrgService sysOrgService;

    /**
     * 查询所有衣服
     */
    @GetMapping("/all")
    @ApiOperation(value = "查询所有衣服")
    public R all(@RequestParam(required = false) Long clothTypeId) {
        return R.ok(clothItemService.list(new QueryWrapper<ClothItem>().lambda()
                .eq(ClothItem::getTypeId, clothTypeId)
                .eq(ClothItem::getDelFlag, DeleteStatus.OK.getCode())
                .eq(ClothItem::getStatus, StatusType.OK.getCode())
                .orderByAsc(ClothItem::getSort)));
    }

    /**
     * 查询所有衣服（带默认洗衣方式价格）
     *
     * @param clothTypeId
     * @return
     */
    @GetMapping("/allWithPrice")
    @ApiOperation(value = "查询所有衣服（带默认洗衣方式价格）")
    public R<List<ClothItemWithPriceQueryVO>> allWithPrice(@RequestParam(required = false) Long clothTypeId) {
        return R.ok(clothItemService.allWithPrice(clothTypeId));
    }

    /**
     * 查询衣服列表
     */
//    @PreAuthorize("@ss.hasPermi('cloth:clothItem:list')")
    @GetMapping("/list")
    @ApiOperation(value = "查询衣服列表")
    public TableDataInfo list(ClothItem clothItem) {
        startPage();
        List<ClothItemVo> list = clothItemService.selectClothItemList(clothItem);
        return getDataTable(list);
    }

    /**
     * 导出衣服列表
     */
    @PreAuthorize("@ss.hasPermi('cloth:clothItem:export')")
    @Log(title = "衣服", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出衣服列表")
    public void export(HttpServletResponse response, ClothItem clothItem) {
        List<ClothItemVo> list = clothItemService.selectClothItemList(clothItem);
        ExcelUtil<ClothItemVo> util = new ExcelUtil<ClothItemVo>(ClothItemVo.class);
        util.exportExcel(response, list, "衣服数据");
    }

    /**
     * 获取衣服详细信息
     */
    @PreAuthorize("@ss.hasPermi('cloth:clothItem:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "获取衣服详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(clothItemService.selectClothItemById(id));
    }

    /**
     * 新增衣服
     */
    @PreAuthorize("@ss.hasPermi('cloth:clothItem:add')")
    @Log(title = "衣服", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增衣服")
    public AjaxResult add(@RequestBody ClothItem clothItem) {
        clothItem.setCreateById(getUserId());
        clothItem.setCreateBy(getUsername());
        return toAjax(clothItemService.insertClothItem(clothItem));
    }

    /**
     * 修改衣服
     */
    @PreAuthorize("@ss.hasPermi('cloth:clothItem:edit')")
    @Log(title = "衣服", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改衣服")
    public AjaxResult edit(@RequestBody ClothItem clothItem) {
        clothItem.setUpdateById(getUserId());
        clothItem.setUpdateBy(getUsername());
        return toAjax(clothItemService.updateClothItem(clothItem));
    }

    /**
     * 删除衣服
     */
    @PreAuthorize("@ss.hasPermi('cloth:clothItem:remove')")
    @Log(title = "衣服", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "删除衣服")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(clothItemService.deleteClothItemByIds(ids));
    }

    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('cloth:clothItem:edit')")
    @Log(title = "衣服", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody ClothItem clothItem) {
        return toAjax(clothItemService.updateClothItemStatus(clothItem));
    }

    @GetMapping("/priceList")
    @ApiOperation(value = "查询门店洗护价格列表")
    public TableDataInfo priceList(ClothItem clothItem) {
        SysOrg sysOrg = sysOrgService.findById(getLoginUser().getOrgId(), getLoginUser().getSourceType());
        startPage();
        List<ClothPriceSetVO> list = clothItemService.selectClothItemPriceList(sysOrg.getBindAreaId(), clothItem);
        return getDataTable(list);
    }

    @GetMapping("/priceListByAreaId")
    @ApiOperation(value = "查询门店洗护价格列表")
    public TableDataInfo priceList(ClothItem clothItem, Long areaId) {
        startPage();
        List<ClothPriceSetVO> list = clothItemService.selectClothItemPriceList(areaId, clothItem);
        return getDataTable(list);
    }

    @GetMapping("/priceListByWahMode")
    @ApiOperation(value = "查询洗护价格列表")
    public TableDataInfo priceListByWahMode(ClothItem clothItem) {
        startPage();
        List<ClothPriceSetVO> list = clothItemService.priceListByWahMode(clothItem);
        return getDataTable(list);
    }
}
