package com.jwipc.sys.controller.pc;

import com.jwipc.page.PageView;
import com.jwipc.security.LoginUtil;
import com.jwipc.sys.dto.FactoryDTO;
import com.jwipc.sys.dto.UserFactoryTreeDTO;
import com.jwipc.sys.logic.FactoryLogic;
import com.jwipc.sys.model.query.FactoryQuery;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.jwipc.sys.constant.ModuleConst.MODULE_ROOT;

/**
 * @author shenyang.ou
 * @date 2024/1/29 14:53
 * @desc 工厂
 */
@RestController
@RequestMapping(MODULE_ROOT + "/factory/rest/")
@RequiredArgsConstructor
public class   FactoryRestController {
    private final FactoryLogic factoryLogic;

    /**
     * 列表
     *
     * @param query
     * @return
     */
    @RequestMapping("/list")
    public PageView list(FactoryQuery query) {
        return factoryLogic.findByPage(query);
    }

    /**
     * 列表
     *
     * @param query
     * @return
     */
    @RequestMapping("/listAll")
    public ResponseEntity<List<FactoryDTO>> listAll(FactoryQuery query) {
        if (query.getIsAuthority()!=null){
            query.setUserId(query.getIsAuthority()==1? LoginUtil.getUserId():null);
        }
        return ResponseEntity.ok(factoryLogic.listAll(query));
    }


    /**
     * 保存更新
     *
     * @param param 参数
     */
    @PostMapping("/saveOrUpdate")
    public ResponseEntity saveOrUpdate(FactoryDTO param) {
        factoryLogic.saveOrUpdate(param);
        return ResponseEntity.ok(true);
    }

    /**
     * 删除
     *
     * @param factoryCode 参数
     */
    @PostMapping("/remove")
    public ResponseEntity remove(String factoryCode) {
        factoryLogic.remove(factoryCode);
        return ResponseEntity.ok(true);
    }

    @GetMapping("getForTree")
    public ResponseEntity getForTree(Long userId) {
        List<UserFactoryTreeDTO> list = factoryLogic.getForTree(userId);
        return ResponseEntity.ok(list);
    }

    /**
     * 列表
     *
     * @return
     */
    @RequestMapping("/allList")
    public ResponseEntity allList() {
        return ResponseEntity.ok(factoryLogic.findByAllList());
    }

}
