package com.zcxk.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcxk.common.FileUploadUtil;
import com.zcxk.dto.TenantDto;
import com.zcxk.logAdvice.LogAnnotation;
import com.zcxk.model.AjaxMessage;
import com.zcxk.model.Pagination;
import com.zcxk.model.ResultStatus;
import com.zcxk.service.TenantService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

 /**
 * @description 租户(Tenant)控制层
 * @auto wangli
 * @data 2020-10-26 15:44
 */
@RestController
@RequestMapping("/tenant")
@Api(tags = "租户管理")
public class TenantController {


    /**
     * 租户
     */
    @Autowired
    private TenantService tenantService;

    @Value("${UMIS.sys_config_path}")
    private String baseDir;

    /**
     * 通过主键查询单条数据
     *
     * @param id
     * @return 单条数据
     */
    @RequestMapping(value = "get", method = RequestMethod.POST)
    @ApiOperation(value = "根据id查询租户")
    public AjaxMessage<TenantDto> selectOne(
            @ApiParam(value = "租户信息", required = true) @RequestParam Integer id) {
        TenantDto result = tenantService.selectById(id);

        return new AjaxMessage<>(ResultStatus.OK, result);
    }

     /**
      * 通过租户标识查询单条数据
      *
      * @param code
      * @return 单条数据
      */
     @RequestMapping(value = "getByCode", method = RequestMethod.POST)
     @ApiOperation(value = "根据id查询租户")
     public AjaxMessage<TenantDto> selectOneByCode(
             @ApiParam(value = "租户标识", required = true) @RequestParam String code) {
         TenantDto result = tenantService.selectByCode(code);

         return new AjaxMessage<>(ResultStatus.OK, result);
     }
    /**
     * 查询租户信息
     *
     * @param tenantDto 参数对象
     * @return 多条数据
     */
    @RequestMapping(value = "selectList", method = RequestMethod.POST)
    @ApiOperation(value = "查询租户信息")
    public AjaxMessage<List<TenantDto>> selectList(
            @ApiParam(value = "租户信息") @RequestBody TenantDto tenantDto) {
        List<TenantDto> result = tenantService.selectList(tenantDto);

        return new AjaxMessage<>(ResultStatus.OK, result);
    }

    /**
     * 新增租户logo
     */
    @RequestMapping(value = "addTenantLogo", method = RequestMethod.POST)
    @ApiOperation(value = "租户logo")
    public  AjaxMessage<String>  addUserPhoto(@ApiParam(value = "租户logo", required = true)@RequestParam("avatarfile") MultipartFile file) {
        String avatar = "";
        if (!file.isEmpty()) {
            try {
                avatar = FileUploadUtil.uploadWeb(baseDir, file);
            } catch (IOException e) {
                return new AjaxMessage<>(ResultStatus.ERROR, e.getMessage());
            }
        }
        return new AjaxMessage<>(ResultStatus.OK, avatar);
    }

    /**
     * 新增一条数据
     *
     * @param tenantDto 实体类
     * @return Response对象t
     */
    @LogAnnotation(module = "【租户管理】新增")
    @RequestMapping(value = "insert", method = RequestMethod.POST)
    @ApiOperation(value = "插入租户信息")
    public AjaxMessage<Integer> insert(@ApiParam(value = "租户信息", required = true) @RequestBody TenantDto tenantDto) {
        if (tenantDto.getTenantName() != null) {
            boolean isExsit = tenantService.checkTenantNameUnique(tenantDto);
            if (isExsit) {
                return new AjaxMessage<>(ResultStatus.TENANT_NAME_ALREADY_EXISTS, 0);
            }
        }
        int result = tenantService.insert(tenantDto);

        return new AjaxMessage<>(ResultStatus.OK, result);
    }

    /**
     * 修改一条数据
     *
     * @param tenantDto 实体类
     * @return Response对象
     */
    @LogAnnotation(module = "【租户管理】修改")
    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ApiOperation(value = "修改租户信息")
    public AjaxMessage<Integer> update(@ApiParam(value = "租户信息", required = true) @RequestBody TenantDto tenantDto) {
        if (tenantDto.getTenantName() != null) {
            boolean isExsit = tenantService.checkTenantNameUnique(tenantDto);
            if (isExsit) {
                return new AjaxMessage<>(ResultStatus.TENANT_NAME_ALREADY_EXISTS, 0);
            }
        }

        int result = tenantService.update(tenantDto);
        return new AjaxMessage<>(ResultStatus.OK, result);

    }

    /**
     * 单条删除数据
     *
     * @param id
     * @return Response对象
     */
    @LogAnnotation(module = "【租户管理】删除")
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    @ApiOperation(value = "删除租户信息")
    public AjaxMessage<Integer> delete(@ApiParam(value = "租户信息", required = true) @RequestParam Integer id) {
        int result = tenantService.deleteById(id);
        return new AjaxMessage<>(ResultStatus.OK, result);
    }


    /**
     * 分页查询
     *
     * @param pageNum  偏移
     * @param pageSize 条数
     * @return Response对象
     */
    @RequestMapping(value = "selectPage", method = RequestMethod.POST)
    @ApiOperation(value = "查询租户信息列表")
    public AjaxMessage<Pagination<TenantDto>> selectPage(
            @ApiParam(value = "租户名称")  @RequestParam(required = false) String tenantName,
            @ApiParam(value = "页数,非必传，默认第一页",  defaultValue = "1") @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @ApiParam(value = "条数，非必传，默认12条",  defaultValue = "12") @RequestParam(required = false, defaultValue = "12") Integer pageSize

    ) {
        TenantDto tenantDto = new TenantDto();
        tenantDto.setTenantName(tenantName);
        IPage<TenantDto> iPage = new Page<>(pageNum, pageSize);
        if(iPage.orders().size() == 0){
            OrderItem orderItem =new OrderItem();
            orderItem.setAsc(false);
            orderItem.setColumn("t.date_create");
            iPage.orders().add(orderItem);
        }
        iPage = tenantService.selectPage(iPage, tenantDto );

        Pagination<TenantDto> pages = new Pagination<>(iPage);

        return new AjaxMessage<>(ResultStatus.OK, pages);
    }

}