package com.ruoyi.web.controller.mch;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.ValidGroup;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ss.store.service.StoreValidator;
import com.ruoyi.ss.storeStaff.domain.StoreStaff;
import com.ruoyi.ss.storeStaff.domain.StoreStaffQuery;
import com.ruoyi.ss.storeStaff.domain.StoreStaffVO;
import com.ruoyi.ss.storeStaff.service.StoreStaffConverter;
import com.ruoyi.ss.storeStaff.service.StoreStaffService;
import com.ruoyi.ss.storeStaff.service.StoreStaffValidator;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wjh
 * 2024/11/13
 */
@RestController
@RequestMapping("/mch/storeStaff")
public class MchStoreStaffController extends BaseController {

    @Autowired
    private StoreStaffService storeStaffService;

    @Autowired
    private StoreValidator storeValidator;

    @Autowired
    private StoreStaffValidator storeStaffValidator;

    @Autowired
    private StoreStaffConverter storeStaffConverter;

    @ApiOperation("商户查询店铺员工列表")
    @GetMapping("/list")
    public TableDataInfo list(StoreStaffQuery query) {
        startPage();
        startOrderBy();
        query.setMchId(getUserId());
        List<StoreStaffVO> list = storeStaffService.selectStoreStaffList(query);
        return getDataTable(list);
    }

    @ApiOperation("商户获取店铺员工详细信息")
    @GetMapping(value = "/{employId}")
    public AjaxResult getInfo(@PathVariable("employId") Long employId)
    {
        StoreStaffQuery query = new StoreStaffQuery();
        query.setMchId(getUserId());
        query.setEmployId(employId);
        return success(storeStaffService.selectOne(query));
    }

    @ApiOperation("商户新增店铺员工")
    @Log(title = "店铺员工", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated(ValidGroup.FrontCreate.class) StoreStaff data) {
        // 校验店铺是否为本人的
        if (!storeValidator.isStoreBelongUser(data.getStoreId(), getUserId())) {
            return error("您只能操作自己的店铺");
        }
        StoreStaff po = storeStaffConverter.toPoByApp(data);
        po.setCreateBy(getUsername());
        po.setCreateId(getUserId());
        return toAjax(storeStaffService.insertStoreStaff(po));
    }

    @ApiOperation("商户修改店铺员工")
    @Log(title = "店铺员工", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated(ValidGroup.FrontUpdate.class) StoreStaff data) {
        if (data.getStoreId() != null) {
            // 校验店铺是否为本人的
            if (!storeValidator.isStoreBelongUser(data.getStoreId(), getUserId())) {
                return error("您只能操作自己的店铺");
            }
        }
        StoreStaff po = storeStaffConverter.toPoByApp(data);
        return toAjax(storeStaffService.updateStoreStaff(po));
    }

    @ApiOperation("商户删除店铺员工")
    @Log(title = "店铺员工", businessType = BusinessType.DELETE)
    @DeleteMapping("/{employIds}")
    public AjaxResult remove(@PathVariable List<Long> employIds) {
        if (!storeStaffValidator.isMch(employIds, getUserId())) {
            return error("您只能操作自己店铺的员工");
        }
        return toAjax(storeStaffService.deleteStoreStaffByEmployIds(employIds));
    }

}
