package com.sy.coinage.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sy.coinage.core.annotation.CurrentUser;
import com.sy.coinage.core.aop.annotation.Operation;
import com.sy.coinage.core.constants.Constants;
import com.sy.coinage.core.controller.BaseController;
import com.sy.coinage.core.interceptor.Create;
import com.sy.coinage.core.interceptor.Update;
import com.sy.coinage.core.req.IdReq;
import com.sy.coinage.core.vo.CacheUserVO;
import com.sy.coinage.sys.model.Region;
import com.sy.coinage.sys.req.RegionListReq;
import com.sy.coinage.sys.req.RegionReq;
import com.sy.coinage.sys.service.IRegionService;
import com.sy.coinage.sys.vo.RegionVO;
import io.swagger.annotations.Api;
import javax.validation.Valid;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 省市区Controller
 */
@Api(value = "省市区接口", tags = "018_省市区")
@RestController
@RequestMapping(value = "/region")
public class RegionController extends BaseController {

    @Autowired
    private IRegionService regionService;

    /**
     * 省市区详情
     */
    @PostMapping("/get")
    public RegionVO get(@RequestBody @Valid IdReq req) {
        RegionVO data = null;
        Region model = this.regionService.getById(req.getId());
        data = new RegionVO();
        BeanUtils.copyProperties(model, data);
        return data;
    }

    /**
     * 省市区分页列表
     */
    @PostMapping("/list")
    public IPage<Region> list(@RequestBody RegionListReq req) {
        LambdaQueryWrapper<Region> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(req.getName())) {
            queryWrapper.like(Region::getName, Constants.L + req.getName() + Constants.L);
        }
        if (req.getLevels() != null && req.getLevels() > 0) {
            queryWrapper.eq(Region::getLevels, Constants.L + req.getName() + Constants.L);
        }
        Page<Region> page = new Page<>(req.getPage(), req.getRows());
        IPage<Region> data = this.regionService.page(page, queryWrapper);
        return data;
    }

    /**
     * 省市区新增
     */
    @Operation(value = "新增省市区")
    @PostMapping("/add")
    public Object add(@RequestBody @Validated({Create.class}) RegionReq req, @CurrentUser CacheUserVO user) {
        // 验证是否租户管理员
        this.validAdmin(user);
        Region model = new Region();
        BeanUtils.copyProperties(req, model);
        this.regionService.save(model);
        return "";
    }

    /**
     * 省市区修改
     */
    @Operation(value = "修改省市区")
    @PostMapping("/update")
    public Object update(@RequestBody @Validated({Update.class}) RegionReq req, @CurrentUser CacheUserVO user) {
        // 验证是否租户管理员
        this.validAdmin(user);
        Region model = this.regionService.getById(req.getCode());
        BeanUtils.copyProperties(req, model);
        this.regionService.updateById(model);
        return "";
    }

    /**
     * 省市区修改状态
     */
    @Operation(value = "修改省市区状态")
    @PostMapping("/status")
    public Integer status(@RequestBody @Valid IdReq req, @CurrentUser CacheUserVO user) {
        // 验证是否租户管理员
        this.validAdmin(user);
        Region model = this.regionService.getById(req.getId());
        if (model.getStatus() != null && model.getStatus().intValue() == Constants.STATUS_FORBIDDEN) {
            model.setStatus(Constants.STATUS_USABLE);
        } else {
            model.setStatus(Constants.STATUS_FORBIDDEN);
        }
        this.regionService.updateById(model);
        return model.getStatus();
    }

    /**
     * 省市区列表项
     */
    @GetMapping("/opt")
    public Object opt() {
        // List<OptVO> data = this.regionService.opt();
        // return data;
        return "";
    }

}
