package com.yiqi.web.controller.system;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yiqi.common.core.domain.R;
import com.yiqi.common.enums.SourceType;
import com.yiqi.common.enums.StatusType;
import com.yiqi.common.utils.SecurityUtils;
import com.yiqi.common.utils.StringUtils;
import com.yiqi.system.domain.SysOrg;
import com.yiqi.system.domain.vo.OrgVO;
import com.yiqi.system.domain.vo.SysStoreVo;
import com.yiqi.system.service.ISysOrgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
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.system.domain.SysStore;
import com.yiqi.system.service.ISysStoreService;
import com.yiqi.common.core.page.TableDataInfo;

/**
 * 门店Controller
 *
 * @author ruoyi
 * @date 2023-07-25
 */
@Api(tags = "门店管理")
@RestController
@RequestMapping("/system/store")
public class SysStoreController extends BaseController {
    @Autowired
    private ISysStoreService sysStoreService;
    @Autowired
    private ISysOrgService sysOrgService;

    @ApiOperation("查询所有组织信息")
    @GetMapping("/allOrg")
    public R<List<SysOrg>> allOrg(@RequestParam(required = false) @ApiParam("来源类型") String sourceType){
        LambdaQueryWrapper<SysOrg> queryWrapper = new QueryWrapper<SysOrg>().lambda().eq(SysOrg::getStatus, StatusType.OK.getCode())
                .orderByAsc(SysOrg::getSourceType);
        if(StringUtils.isNotEmpty(sourceType)){
            queryWrapper.eq(SysOrg::getSourceType,sourceType);
        }
        return R.ok(sysOrgService.list(queryWrapper));
    }

    /**
     * 查询门店列表
     */
    @ApiOperation("查询门店列表")
    @PreAuthorize("@ss.hasPermi('system:store:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysStore sysStore) {
        startPage();
        if(getLoginUser().getSourceType().equals("01")){
            sysStore.setFactoryId(getLoginUser().getOrgId());
        }
        List<SysStoreVo> list = sysStoreService.selectSysStoreList(sysStore);
        return getDataTable(list);
    }

    /**
     * 获取门店详细信息
     */
    @ApiOperation("获取门店详细信息")
    @PreAuthorize("@ss.hasPermi('system:store:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(sysStoreService.selectSysStoreById(id));
    }

    /**
     * 新增门店
     */
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("新增门店")
    @PreAuthorize("@ss.hasPermi('system:store:add')")
    @Log(title = "门店", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysStore sysStore) {
        return toAjax(sysStoreService.insertSysStore(sysStore));
    }

    /**
     * 修改门店
     */
    @ApiOperation("修改门店")
    @PreAuthorize("@ss.hasPermi('system:store:edit')")
    @Log(title = "门店", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysStore sysStore) {
        return toAjax(sysStoreService.updateSysStore(sysStore));
    }

    /**
     * 删除门店
     */
    @ApiOperation("删除门店")
    @PreAuthorize("@ss.hasPermi('system:store:remove')")
    @Log(title = "门店", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysStoreService.deleteSysStoreByIds(ids));
    }

    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('system:store:edit')")
    @Log(title = "门店", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody SysStore sysStore) {
        sysStore.setUpdateBy(getUsername());
        return toAjax(sysStoreService.updateStoreStatus(sysStore));
    }


    /**
     * 获取单家门店详细信息
     */
    @ApiOperation("获取单家门店详细信息")
    @PreAuthorize("@ss.hasPermi('system:store:query')")
    @GetMapping(value = "/info")
    public AjaxResult getStoreInfo() {
        return success(sysStoreService.selectSysStoreInfoById());
    }


    /**
     * 获取单家门店详细信息
     */
    @ApiOperation("获取某个工厂下面所有门店")
    @GetMapping(value = "/getStoreByFactoryId")
    public R<List<SysStore>> getStoreByFactoryId(){
        if (!SourceType.FACTORY.getCode().equals(SecurityUtils.getLoginUser().getSourceType())){
            return R.ok(new ArrayList<>());
        }
        return R.ok(sysStoreService.list(new QueryWrapper<SysStore>().lambda()
                .eq(SysStore::getFactoryId, SecurityUtils.getLoginUser().getOrgId())
                .eq(SysStore::getDelFlag, StatusType.OK.getCode())
                .eq(SysStore::getStatus, StatusType.OK.getCode())));
    }


}
