package com.ics.admin.controller;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.ics.admin.domain.Customer;
import com.ics.admin.domain.Park;
import com.ics.admin.service.IParkService;
import com.ics.common.core.controller.BaseController;
import com.ics.common.core.domain.R;
import com.ics.common.utils.DateUtils;
import com.ics.common.utils.ValidatorUtils;
import com.ics.system.domain.Dept;
import com.ics.system.domain.User;
import com.ics.system.service.ICurrentUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.wf.jwtp.annotation.Ignore;
import org.wf.jwtp.annotation.Logical;
import org.wf.jwtp.annotation.RequiresPermissions;
import org.wf.jwtp.util.SubjectUtil;

import java.util.List;


/**
 * 园区管理 提供者
 *
 * @author zzm
 * @date 2021-03-23
 */
@RestController
@RequestMapping("admin/park")
public class ParkController extends BaseController {

    @Autowired
    private IParkService parkService;

    @Autowired
    private ICurrentUserService currentUserService;
    /**
     * 查询园区管理
     */
    @RequiresPermissions("admin:park:view")
    @GetMapping("get/{id}")
    public Park get(@PathVariable("id") Long id) {
        return parkService.selectParkById(id);
    }

    /**
     * 查询当前园区
     */
    @RequiresPermissions("admin:park:setting")
    @GetMapping("getCurrentPark")
    public Park getCurrentPark(Park park) {
        return parkService.selectCurrentParkByDeptId(park);
    }

    /**
     * 查询园区管理列表
     */
    @RequiresPermissions(value = {"admin:park:list", "system:user:list"}, logical = Logical.OR)
    @GetMapping("list")
    public R list(Park park) {
        startPage();
        boolean isAdmin = SubjectUtil.hasRole(getRequest(),"manager");
        if (isAdmin){
            Long parkId = currentUserService.getParkId();
            Long tenantId = currentUserService.getTenantId();
            park.setId(parkId);
            park.setTenantId(tenantId);
        }

        return result(parkService.selectParkList(park));
    }


    /**
     * 新增保存园区管理
     */
    @RequiresPermissions("admin:park:add")
    @PostMapping("save")
    public R addSave(@RequestBody Park park) {
        ValidatorUtils.validateEntity(park);
        if (parkService.exists("name", park.getName())) {
            return R.error("园区名称重复!");
        }
        Assert.isTrue(park.getLng().toString().length() <= 10, "经度格式错误");
        Assert.isTrue(park.getLat().toString().length() <= 10, "维度格式错误");
        park.setIsMarketable(false);
        return toAjax(parkService.insertPark(park));
    }

    /**
     * 修改保存园区管理
     */
    @RequiresPermissions("admin:park:edit")
    @PostMapping("update")
    public R editSave(@RequestBody Park park) {
        ValidatorUtils.validateEntity(park);
        if (parkService.unique(park.getId(), "name", park.getName())) {
            return R.error("园区名称重复!");
        }
        return toAjax(parkService.updatePark(park));

    }


    /**
     * 园区状态修改
     */
    @RequiresPermissions("admin:park:edit")
    @PostMapping("/changeMarketable")
    public R changeMarketable(@RequestBody Park park) {
        Park newPark = parkService.selectParkById(park.getId());
        if (newPark == null) {
            return R.error("园区不存在!");
        }
        newPark.setMarketableTime(DateUtils.getNowDate());
        //修改上架时间
        newPark.setIsMarketable(park.getIsMarketable());
        return toAjax(parkService.updatePark(newPark));
    }

    /**
     * 删除园区管理
     */
    @RequiresPermissions("admin:park:remove")
    @PostMapping("remove")
    public R remove(String ids) {
        return toAjax(parkService.deleteParkByIds(ids));
    }


    /**
     * 初始化园区信息
     */
    @RequiresPermissions("admin:park:add")
    @PostMapping("initSavePark")
    public R initSavePark(@RequestBody Dept dept) {
        dept.setCreateBy(getLoginName());
        if (parkService.exists("name", dept.getDeptName())) {
            return R.error("园区名称重复!");
        }
        return toAjax(parkService.initPark(dept));
    }


    /**
     * 获取园区列表
     */
    @Ignore
    public R selectParkList(Park park) {

        return R.data(parkService.selectParkList(park));
    }

}