package com.storlead.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.storlead.framework.common.result.Result;
import com.storlead.user.pojo.entity.SubCompanyEntity;
import com.storlead.user.service.ISubCompanyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
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 javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/lingcun")
@Api(tags="5.公共接口")
public class SubCompanyController {

    @Resource
    private ISubCompanyService subCompanyService;

    @GetMapping(value = "/subCompany/list")
    @ApiOperation(value = "公共接口-分公司-列表", notes = "公共接口-分公司-列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "", response = SubCompanyEntity.class)
    })
    public Result<?> list() {
        // Step.1 组装查询条件
        QueryWrapper<SubCompanyEntity> queryWrapper = new QueryWrapper<SubCompanyEntity>();
        queryWrapper.select("id,company_id,`desc`,name,sort,enabled");
        queryWrapper.eq("enabled", 1);
        queryWrapper.orderByAsc("sort");
        List<SubCompanyEntity> list = subCompanyService.list(queryWrapper);
        return Result.ok(list);
    }

    @GetMapping(value = "/subCompany/listHasAllCompany")
    @ApiOperation(value = "公共接口-分公司-包含所有公司列表", notes = "公共接口-分公司-包含所有公司列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "", response = SubCompanyEntity.class)
    })
    public Result<?> listHasAllCompany() {
        List<SubCompanyEntity> list = new ArrayList<>();
        SubCompanyEntity subCompany = new SubCompanyEntity();
        subCompany.setId(0L);
        subCompany.setName("所有公司");
        subCompany.setDesc("所有公司");
        list.add(subCompany);
        // Step.1 组装查询条件
        QueryWrapper<SubCompanyEntity> queryWrapper = new QueryWrapper<SubCompanyEntity>();
        queryWrapper.select("id,company_id,`desc`,name,sort,enabled");
        queryWrapper.eq("enabled", 1);
        queryWrapper.orderByAsc("sort");
        List<SubCompanyEntity> subCompanyEntityList = subCompanyService.list(queryWrapper);
        list.addAll(subCompanyEntityList);
        return Result.ok(list);
    }
    @ApiOperation("启用禁用部门")
    @PostMapping("/su_company/enable")
    public Result setEnable(Long id,Boolean enable) {
        LambdaUpdateWrapper<SubCompanyEntity> update = new LambdaUpdateWrapper();
        update.in(SubCompanyEntity::getId, Arrays.asList(id));
        update.set(SubCompanyEntity::getEnabled,enable);
        Boolean re = subCompanyService.update(update);
        if (re) {
            return Result.ok();
        } else {
            return Result.error("操作失败");
        }
    }

}
