package cn.com.ty.lift.ud.dataBank.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.service.IService;

import cn.com.ty.lift.ud.common.BaseController;
import cn.com.ty.lift.ud.common.DelReq;
import cn.com.ty.lift.ud.dataBank.controller.query.LiftBrandQuery;
import cn.com.ty.lift.ud.dataBank.mapper.entity.LiftBrandEntity;
import cn.com.ty.lift.ud.dataBank.mapper.entity.LiftBrandVo;
import cn.com.ty.lift.ud.dataBank.service.ILiftBrandService;
import cn.com.ty.lift.ud.utils.PingYinUtil;
import cn.com.xwy.boot.web.dto.RestResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 电梯品牌 API
 * 
 * @author Xson
 * @date 2020-04-23
 */
@Api(tags = "电梯品牌 API- 魏瑞锋")
@RestController
@RequestMapping("/api/liftBrands")
public class LiftBrandController extends BaseController<LiftBrandEntity, LiftBrandQuery> {

    private final Logger log = LoggerFactory.getLogger(LiftBrandController.class);

    private @Autowired ILiftBrandService liftBrandService;

    @Override
    protected IService<LiftBrandEntity> getService() {
        return liftBrandService;
    }

    // 新建
    @PostMapping("/insert")
    @ApiOperation(value = "新增")
    public RestResponse<LiftBrandEntity> create(@RequestBody LiftBrandEntity entity) {
        if (!PingYinUtil.isContainChinese(entity.getName())) {
            return RestResponse.fail("请输入中文字符");
        }
        List<LiftBrandEntity> byName = liftBrandService.getByName(entity.getName());
        if (!byName.isEmpty()) {
            return RestResponse.fail("品牌名重复");
        }
        if (StringUtils.isBlank(entity.getCode())) {
            entity.setCode(PingYinUtil.getFirstSpell(entity.getName()));
        }
        boolean saveFlag = getService().save(entity);

        if (saveFlag)
            return RestResponse.success(null, "Created successfully");
        else
            return RestResponse.fail("Created unsuccessfully");
    }

    //修改
    @PutMapping("/update")
    @ApiOperation(value = "修改")
    public RestResponse update(@RequestBody LiftBrandEntity request) {
        List<LiftBrandEntity> byName = liftBrandService.getByName(request.getName());
        if (byName.size()>1) {
            return RestResponse.fail("品牌名重复");
        }
        LiftBrandEntity byId = getService().getById(request.getId());
        if (StringUtils.isBlank(request.getCode()) && StringUtils.isNotBlank(request.getName())) {
            request.setCode(PingYinUtil.getFirstSpell(request.getName()));
        }
        boolean updateFlag = getService().updateById(request);
        if (updateFlag)
            return RestResponse.success(null, "Updated successfully");
        else
            return RestResponse.fail("Updated unsuccessfully");
    }
    
    @PostMapping("/brandListApp")
    @ApiOperation(value = "app品牌列表")
    public RestResponse brandListApp(@RequestBody LiftBrandEntity lt) {
        List<LiftBrandVo> brandListApp = liftBrandService.brandListApp(lt);
        return RestResponse.success(brandListApp, "successfully");
    }

    @ApiOperation(value = "批量上架")
    @PostMapping("useBath")
    public RestResponse useBath(@RequestBody DelReq ids) {
        liftBrandService.updateByIds(ids.getIds(), 1);
        return RestResponse.success("上架成功");
    }

    @ApiOperation(value = "批量下架")
    @PostMapping("noUseBath")
    public RestResponse noUseBath(@RequestBody DelReq ids) {
        liftBrandService.updateByIds(ids.getIds(), 0);
        return RestResponse.success("下架成功");
    }

    @ApiOperation(value = "批量首页显示")
    @PostMapping("showIndex")
    public RestResponse showIndex(@RequestBody DelReq ids) {
        liftBrandService.updateHotFlagByIds(ids.getIds(), 1);
        return RestResponse.success("首页显示");
    }

    @ApiOperation(value = "取消批量首页显示")
    @PostMapping("noshowIndex")
    public RestResponse noshowIndex(@RequestBody DelReq ids) {
        liftBrandService.updateHotFlagByIds(ids.getIds(), 0);
        return RestResponse.success("取消首页显示");
    }

    @PostMapping("/query")
    @ApiOperation(value = "查询所有")
    public RestResponse queryAll() {
        List<LiftBrandEntity> queryAll = liftBrandService.queryAll();
        return RestResponse.success(queryAll, "Queried successfully");
    }

}