package com.zhiqiyun.open.mvc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhiqiyun.open.annotation.Permission;
import com.zhiqiyun.open.core.models.bz.BaseDataInfo;
import com.zhiqiyun.open.core.models.bz.HolidayVillageInfo;
import com.zhiqiyun.open.core.models.bz.SceneryInfo;
import com.zhiqiyun.open.core.models.user.OauthInfo;
import com.zhiqiyun.open.core.service.HolidayVillageInfoService;
import com.zhiqiyun.open.core.service.OauthService;
import com.zhiqiyun.open.core.service.SequenceService;
import com.zhiqiyun.open.mvc.Result;
import com.zhiqiyun.open.mvc.params.bz.QueryHolidayVillageInfoParams;
import com.zhiqiyun.open.mvc.params.bz.SaveHolidayVillageInfoParams;
import com.zhiqiyun.open.utils.DateUtil;
import com.zhiqiyun.open.utils.ServletContext;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
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;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/bz/base/holiday/village")
public class HolidayVillageInfoController {

    @Autowired
    private HolidayVillageInfoService holidayVillageInfoService;

    @Autowired
    private SequenceService sequenceService;

    @Autowired
    private OauthService oauthService;

    @Permission(value = "bz.base.holiday.village.find", tags = "查询度假村")
    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryHolidayVillageInfoParams params) {

        QueryWrapper<HolidayVillageInfo> queryWrapper = new QueryWrapper<>();

        if (StringUtils.isNotBlank(params.getKeyword())) {
            StringBuilder sb = new StringBuilder("select id from bz_base_info where");
            sb.append(" name like ").append("'%").append(params.getKeyword()).append("%'");
            sb.append(" or brief like ").append("'%").append(params.getKeyword()).append("%'");
            sb.append(" or address like ").append("'%").append(params.getKeyword()).append("%'");
            sb.append(" or contacts like ").append("'%").append(params.getKeyword()).append("%'");
            sb.append(" or contact_number like ").append("'%").append(params.getKeyword()).append("%'");
            queryWrapper.inSql("id", sb.toString());
        }

        Page<HolidayVillageInfo> page = params.getPage();
        page.addOrder(OrderItem.desc("id"));

        Page<HolidayVillageInfo> resultData = this.holidayVillageInfoService.page(page, queryWrapper);

        return Result.instance(Result.Code.SUCCESS).setData(resultData);
    }

    @Permission(value = "bz.base.holiday.village.add", tags = "保存度假村")
    @PostMapping("/save")
    public Result save(@Valid @RequestBody SaveHolidayVillageInfoParams params) {

        this.holidayVillageInfoService.save(this.buildEntity(this.sequenceService.nextId(), params));

        return Result.instance(Result.Code.MESSAGE_SUCCESS);
    }

    @Permission(value = "bz.base.holiday.village.edit", tags = "编辑度假村")
    @PostMapping("/updateById")
    public Result updateById(Long id, @Valid @RequestBody SaveHolidayVillageInfoParams params) {

        this.holidayVillageInfoService.updateById(this.buildEntity(id, params));

        return Result.instance(Result.Code.MESSAGE_SUCCESS);
    }

    private HolidayVillageInfo buildEntity(Long id, @Valid SaveHolidayVillageInfoParams params) {

        HolidayVillageInfo entity = new HolidayVillageInfo();
        BaseDataInfo baseDataInfo = new BaseDataInfo();

        BeanUtils.copyProperties(params, entity);
        BeanUtils.copyProperties(params.getBaseDataInfo(), baseDataInfo);


        OauthInfo oauthInfo = this.oauthService.getAuth(ServletContext.getAccessToken());

        baseDataInfo.setCreatedTime(DateUtil.current());
        baseDataInfo.setUpdatedTime(DateUtil.current());
        baseDataInfo.setCreatedBy(oauthInfo.getId());
        baseDataInfo.setUpdatedBy(oauthInfo.getId());

        entity.setBaseDataInfo(baseDataInfo);
        entity.setId(id);
        return entity;
    }

    @Permission(value = "bz.base.holiday.village.delete", tags = "删除度假村")
    @PostMapping("/deleteByIds")
    public Result deleteByIds(@RequestBody List<Long> ids) {
        this.holidayVillageInfoService.removeByIds(ids);
        return Result.instance(Result.Code.MESSAGE_SUCCESS);
    }
}
