package cn.pluss.platform.controller;

import cn.jiguang.common.utils.StringUtils;
import cn.pluss.platform.DeviceGoodsService;
import cn.pluss.platform.api.PageInfo;
import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.constants.ProjectConstant;
import cn.pluss.platform.device.DeviceService;
import cn.pluss.platform.entity.*;
import cn.pluss.platform.merchantDevice.MerchantDeviceService;
import cn.pluss.platform.userApp.UserAppService;
import cn.pluss.platform.util.StringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/merchantDevice")
@Api(value = "MerchantDevice控制类", tags = {"MerchantDevice接口"})
public class MerchantDeviceController {
    @Resource
    private MerchantDeviceService merchantDeviceService;

    @Resource
    private DeviceService deviceService;

    @Setter(onMethod_ = {@Autowired})
    private DeviceGoodsService deviceGoodsService;

    @Resource
    private UserAppService userAppService;

    @GetMapping
    @ApiOperation(value = "获取全部", notes = "返回分页过后的数据", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "查询页码", paramType = "query", dataType = "Integer", defaultValue = "0"),
            @ApiImplicitParam(name = "size", value = "每页数据量", paramType = "query", dataType = "Integer", defaultValue = "0")
    })
    public Result<PageInfo<MerchantDevice>> list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size) {
        if (size == null || size == 0) {
            size = ProjectConstant.PAGE_SIZE;
        }
        if (page == null || page == 0) {
            page = 1;
        }

        Map<String, Object> map = new HashMap<>(16);
        map.put("pageSize", size);
        map.put("offset", (page - 1) * size);
        UserApp userApp = userAppService.queryUserAppByToken();
        map.put("userId", userApp.getUserId());
        List<MerchantDevice> merchantDeviceList = merchantDeviceService.queryMerchantDevicePage(map);
        for (MerchantDevice d : merchantDeviceList) {
            d.setStatus(2);
            DeviceSignUnbind sign = new DeviceSignUnbind();
            sign.setMerchantCode(d.getMerchantCode());
            Device device = new Device();
            if(StringUtils.isNotEmpty(d.getOrderNo() )){
                if(d.getVenderCode() != null){
                    device.setVenderCode(d.getVenderCode());
                    device = deviceService.queryDevice(device);
                    d.setDeviceLogo(device.getDeviceLogo());
                    d.setVenderModel(device.getVenderModel());
                    d.setIntroDesc(device.getIntroDesc());
                    d.setName(device.getName());
                    sign.setVenderCode(d.getVenderCode());
                }
            } else {
                if(d.getVenderCode() != null){
                    device.setVenderCode(d.getVenderCode());
                    device = deviceService.queryDevice(device);
                    d.setDeviceLogo(device.getDeviceLogo());
                    d.setVenderModel(device.getVenderModel());
                    sign.setVenderCode(d.getVenderCode());
                }
            }

            if (StringUtils.isEmpty(d.getDeviceLogo()) && StringUtils.isNotEmpty(d.getSn())) {
                DeviceGoods deviceGoods = deviceGoodsService.getBySn(d.getSn());
                d.setDeviceLogo(deviceGoods.getDeviceLogo());
            }
        }
        Integer count = merchantDeviceService.queryMerchantDevicePageCount(map);
        //返回参数
        PageInfo pager = new PageInfo<>(count.longValue(), (long) StringUtil.getPageCount(count, size), page.longValue(), size.longValue(), merchantDeviceList);
        return ResultGenerator.genSuccessResult(pager);
    }

    @PostMapping
    @ApiOperation(value = "添加数据", notes = "添加新的数据", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "MerchantDevice", value = "待添加的MerchantDevice实例", paramType = "body", dataType = "MerchantDevice", required = true)
    })
    public Result add(@RequestBody MerchantDevice merchantDevice) {
        merchantDeviceService.save(merchantDevice);
        return ResultGenerator.genSuccessResult();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除数据", notes = "根据id删除数据", httpMethod = "DELETE")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "查询的id实例", paramType = "path", dataType = "Integer", required = true)
    })
    public Result delete(@PathVariable Long id) {
        merchantDeviceService.removeById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PutMapping
    @ApiOperation(value = "更新数据", notes = "根据内容更新数据", httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "MerchantDevice", value = "更新的MerchantDevice实例", paramType = "body", dataType = "MerchantDevice", required = true)
    })
    public Result update(@RequestBody MerchantDevice merchantDevice) {
        merchantDeviceService.updateById(merchantDevice);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "获取单个值", notes = "查看单个项目的内容", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "查询的id", paramType = "path", required = true, dataType = "Integer", defaultValue = "0")
    })
    public Result detail(@PathVariable Long id) {
        MerchantDevice merchantDevice = new MerchantDevice();
        merchantDevice.setId(id);
        merchantDevice = merchantDeviceService.getOne(new QueryWrapper<>(merchantDevice));
        return ResultGenerator.genSuccessResult(merchantDevice);
    }

}

