package com.ningdatech.carapi.sys.controller;

import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.ningdatech.basic.model.PageVo;
import com.ningdatech.carapi.sys.entity.Company;
import com.ningdatech.carapi.sys.entity.dto.*;
import com.ningdatech.carapi.sys.entity.vo.CompanyVO;
import com.ningdatech.carapi.sys.helper.CompaniesCacheHelper;
import com.ningdatech.carapi.sys.manage.CompanyManage;
import com.ningdatech.carapi.sys.service.CompanyService;
import com.ningdatech.carapi.user.util.LoginUserUtil;
import com.ningdatech.log.annotation.WebLog;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 前端控制器
 * 企业管理
 * </p>
 *
 * @author PoffyZhang
 * @since 2022-10-09
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/api/company")
@Api(value = "Company", tags = "系统管理-企业")
@RequiredArgsConstructor
public class CompanyController {

    private final CompanyManage companyManage;
    private final CompanyService companyService;
    private final CompaniesCacheHelper companiesCacheHelper;

    @GetMapping("/testCache")
    public CompanyDTO testCache(@Valid @RequestParam Long id) {
        return companiesCacheHelper.getById(id);
    }

    @GetMapping("/asMap")
    public Map<Long, CompanyDTO> testCache() {
        return companiesCacheHelper.asMap();
    }

    @GetMapping("/refresh")
    public String refresh() {
        companiesCacheHelper.afterPropertiesSet();
        return "success";
    }

    @ApiOperation(value = "根据地区ID集合 查询企业ID集合", notes = "根据地区ID集合 查询企业ID集合")
    @PostMapping("/getIdsByRegionIds")
    public List<Long> getIdsByRegionIds(@Valid @RequestBody List<Long> regionIds) {
        return companyManage.getIdsByRegionIds(regionIds);
    }

    @ApiOperation(value = "企业类型级联", notes = "企业类型级联")
    @GetMapping("/types")
    public List<CascadeDTO> queryTypes() {
        return companyManage.queryCompanyTypes();
    }

    @ApiOperation(value = "企业列表查询", notes = "企业列表查询")
    @GetMapping("/list")
    public PageVo<CompanyVO> queryList(@Valid @ModelAttribute CompanyPageQuery companyPageQuery) {
        return companyManage.queryList(companyPageQuery);
    }

    @ApiOperation(value = "使用企业列表查询", notes = "使用企业列表查询")
    @GetMapping("/useCompanyList")
    public PageVo<CompanyVO> queryUseCompanyList(@Valid @ModelAttribute CompanyPageQuery companyPageQuery) {
        return companyManage.queryUseCompanyList(companyPageQuery);
    }

    /**
     * 查询企业详情
     *
     * @param id 主键id
     * @return 查询结果
     */
    @ApiOperation(value = "查询企业详情", notes = "查询企业详情")
    @GetMapping("/details")
    public CompanyQueryDTO getDetails(@Valid @RequestParam Long id) {
        return companyManage.getDetails(id);
    }

    @ApiOperation(value = "保存新企业", notes = "保存新企业")
    @PostMapping("/save")
    @WebLog(value = "新增企业", modular = "系统管理-企业管理")
    public CompanyVO handlerSave(@Valid @RequestBody CompanySaveDTO data) {
        Company company =  companyManage.save(data, LoginUserUtil.getUserId());
        return BeanUtil.toBean(company, CompanyVO.class);
    }

    @ApiOperation(value = "编辑企业", notes = "编辑企业")
    @PostMapping("/modify")
    @WebLog(value = "编辑企业", modular = "系统管理-企业管理")
    public CompanyVO handlerUpdate(@Valid @RequestBody CompanyUpdateDTO data) {
        Company company =  companyManage.update(data, LoginUserUtil.getUserId());
        return BeanUtil.toBean(company, CompanyVO.class);
    }

    @ApiOperation(value = "删除企业", notes = "删除企业")
    @PostMapping("/remove")
    @WebLog(value = "删除企业", modular = "系统管理-企业管理")
    public Boolean handlerDelete(@Valid @RequestBody List<Long> ids) {
        return companyManage.removeByIdWithCache(ids);
    }

}
