package com.hzs.sale.product.controller.manage;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.enums.*;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.core.web.page.TableDataInfo;
import com.hzs.common.domain.sale.classify.BdSpecs;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.sale.product.service.IBdSpecsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


/**
 * 规格controller
 */
@Slf4j
@RestController
@RequestMapping("/manage/specs")
public class BdSpecsController extends BaseController {

    @Autowired
    private IBdSpecsService iBdSpecsService;

    /**
     * 查询规格列表
     */
    @GetMapping("/list")
    @Log(module = EOperationModule.SPECS_LIST, business = EOperationBusiness.SPECIFICATIONS_CONFIG, method = EOperationMethod.SELECT)
    public TableDataInfo list(BdSpecs bdSpecs) {
        LambdaQueryWrapper<BdSpecs> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(bdSpecs.getSpecsName())) {
            queryWrapper.eq(BdSpecs::getSpecsName, bdSpecs.getSpecsName());
        }
        startPage();
        List<BdSpecs> list = iBdSpecsService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 新增规格
     */
    @PostMapping("/save")
    @Log(module = EOperationModule.SPECS_LIST, business = EOperationBusiness.SPECIFICATIONS_CONFIG, method = EOperationMethod.INSERT)
    public AjaxResult save(@RequestBody BdSpecs bdSpecs) {
        bdSpecs.setPkCreator(SecurityUtils.getUserId());
        bdSpecs.setCreationTime(new Date());
        bdSpecs.setPkCountry(SecurityUtils.getPkCountry());
        iBdSpecsService.save(bdSpecs);
        return AjaxResult.success();
    }

    /**
     * 修改规格
     */
    @PostMapping("/update")
    @Log(module = EOperationModule.SPECS_LIST, business = EOperationBusiness.SPECIFICATIONS_CONFIG, method = EOperationMethod.UPDATE)
    public AjaxResult update(@RequestBody BdSpecs bdSpecs) {
        bdSpecs.setPkModified(SecurityUtils.getUserId());
        bdSpecs.setModifiedTime(new Date());
        iBdSpecsService.updateById(bdSpecs);
        return AjaxResult.success();
    }

    /**
     * 删除规格
     */
    @DeleteMapping("/{pkId}")
    @Log(module = EOperationModule.SPECS_LIST, business = EOperationBusiness.SPECIFICATIONS_CONFIG, method = EOperationMethod.DELETE)
    public AjaxResult delete(@PathVariable Integer pkId) {
        BdSpecs specs = new BdSpecs();
        specs.setPkId(pkId);
        specs.setDelFlag(EDelFlag.DELETE.getValue());
        specs.setModifiedTime(new Date());
        specs.setPkModified(SecurityUtils.getUserId());
        return toAjax(iBdSpecsService.updateById(specs));
    }

    /**
     * 查询规格详情
     */
    @GetMapping("/detail")
    public AjaxResult detail(Long specsId) {
        return AjaxResult.success(iBdSpecsService.getById(specsId));
    }

    /**
     * 修改规格状态
     */
    @PostMapping("/updateEnable/{pkId}/{enable}")
    @Log(module = EOperationModule.SPECS_LIST, business = EOperationBusiness.SPECIFICATIONS_CONFIG, method = EOperationMethod.UPDATE)
    public AjaxResult updateEnable(@PathVariable Integer pkId, @PathVariable Integer enable) {
        BdSpecs bdSpecs = new BdSpecs();
        bdSpecs.setEnableState(enable);
        bdSpecs.setPkId(pkId);
        bdSpecs.setPkModified(SecurityUtils.getUserId());
        bdSpecs.setModifiedTime(new Date());
        return toAjax(iBdSpecsService.updateById(bdSpecs));
    }

    /**
     * 查询规格列表
     */
    @GetMapping("/find-all")
    @Log(module = EOperationModule.SPECS_LIST, business = EOperationBusiness.SPECIFICATIONS_CONFIG, method = EOperationMethod.SELECT)
    public AjaxResult findAll() {
        List<BdSpecs> list = iBdSpecsService.list();
        return AjaxResult.success(list);
    }

}
