package com.tofly.feesapi.wxgl.wechatofficalacctmgt.controller_easy;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.tofly.feesapi.common.core.ResultRespone;
import com.tofly.feesapi.common.BaseController;
import com.tofly.feesapi.common.dbhelper.DbHelper;
import com.tofly.feesapi.common.dbhelper.PageSortHelper;
import com.tofly.feesapi.common.enums.SysEnum;
import com.tofly.feesapi.common.model.PageLink;
import com.tofly.feesapi.common.util.DateUtils;
import com.tofly.feesapi.common.util.ToolsUtils;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.entity.WxBzsq;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.entity.vo.WxBzsqVo;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.service.WxBzsqService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.text.MessageFormat;
import java.util.Date;

import static com.tofly.feesapi.common.ControllerConstants.*;


@RestController
@RequestMapping("/api/wechatofficalacctmgt/wxbzsq")
@Api(tags = "微信公众号-报装申请接口")
public class WxBzsqEasyController extends BaseController {

    @Autowired
    private WxBzsqService wxBzsqService;

    @GetMapping("/pageInfo")
    @ApiOperation(value = "[Easy]自定义分页查询")
    public ResultRespone getWxBzsqPage(@ApiParam(value = PAGE_SIZE_DESCRIPTION, required = true)
                                       @RequestParam(value = PAGE_SIZE_PARA_NAME) int pageSize,
                                       @ApiParam(value = PAGE_NUMBER_DESCRIPTION, required = true)
                                       @RequestParam(value = PAGE_NUMBER_PARA_NAME) int page,
                                       @ApiParam(value = "状态")
                                       @RequestParam(value = "state", defaultValue = "") String state,
                                       @ApiParam(value = "起始时间") @RequestParam("startTime") @DateTimeFormat(pattern = "yyyy-MM-dd")
                                       Date startTime,
                                       @ApiParam(value = "截至时间") @RequestParam("endTime") @DateTimeFormat(pattern = "yyyy-MM-dd")
                                       Date endTime,
                                       @ApiParam(value = "类型")
                                       @RequestParam(value = "type", defaultValue = "") String type,
                                       @ApiParam(value = "模糊查询")
                                       @RequestParam(value = "searchText", defaultValue = "") String searchText,
                                       @ApiParam(value = SORT_PROPERTY_DESCRIPTION)
                                       @RequestParam(required = false) String sortProperty,
                                       @ApiParam(value = SORT_ORDER_DESCRIPTION, allowableValues = SORT_ORDER_ALLOWABLE_VALUES)
                                       @RequestParam(required = false) String sortOrder
    ) throws Exception {
        DbHelper db = DbHelper.getDbHelper();
        PageLink pg = createPageLink(pageSize, page);

        String strSql = "select * from WX_BZSQ t where 1=1";
        if (!StringUtils.isBlank(state)) {
            strSql += String.format(" and state='%s'", state);
        }

        if (!StringUtils.isBlank(type)) {
            strSql += String.format(" and type='%s'", type);
        }

        if (!StringUtils.isBlank(searchText)) {
            strSql += String.format(" and (REGISTER_NAME like '%1$s%%' or REGISTER_address like '%1$s%%'  or REGISTER_phone like '%1$s%%' or bh like '%1$s%%')", searchText);
        }

        if (startTime != null && endTime != null) {
            strSql += String.format(" and REGISTER_Date>=to_date('%s','yyyy-MM-dd HH24:mi:ss') and REGISTER_Date<=to_date('%s','yyyy-MM-dd HH24:mi:ss')", DateUtils.formatDate(startTime, "yyyy-MM-dd hh:mm:ss"), DateUtils.formatDate(endTime, "yyyy-MM-dd hh:mm:ss"));
        }

        strSql += MessageFormat.format(" order by {0}"
                , PageSortHelper.setPageSort(WxBzsq.class, sortProperty, sortOrder));

        System.out.println(strSql);
        Object obj = db.getPage(pg, strSql, SysEnum.MyBatis.getName());
        return ResultRespone.success(obj);
    }


    @ApiOperation(value = "[Easy]新增")
    @PostMapping("/saveBzsq")
    public ResultRespone saveBzsq(@RequestBody WxBzsq wxBzsq) throws Exception {
        wxBzsq.setId(ToolsUtils.getGuid());
        wxBzsq.setBh(ToolsUtils.getLsbh());
        boolean isAdd = wxBzsqService.save(wxBzsq);
        if (isAdd)
            return ResultRespone.success(wxBzsq.getBh());
        else
            return ResultRespone.failed();
    }

    @ApiOperation(value = "[Easy]业务流程处理")
    @PutMapping("/updateBzsq")
    public ResultRespone updateBzsq(@RequestBody WxBzsqVo wxBzsqVo) throws Exception {
        System.out.println(wxBzsqVo);
        boolean save = wxBzsqService.updateById(wxBzsqVo);
        if (save)
            return ResultRespone.success(true);
        else
            return ResultRespone.failed(false);
    }
}
