package com.tofly.feesapi.yhgl.controller_easy;

import com.tofly.feesapi.common.core.ResultRespone;
import com.tofly.feesapi.common.dbhelper.DbHelper;
import com.tofly.feesapi.common.core.dto.PageDto;
import com.tofly.feesapi.common.core.vo.PageResult;
import com.tofly.feesapi.common.util.DateUtils;
import com.tofly.feesapi.common.util.SqlConvertor;
import com.tofly.feesapi.yhgl.entity.dto.YhkhjbxxFilter;
import com.tofly.feesapi.yhgl.entity.vo.YhkhjbxxFfgxzSelect;
import com.tofly.feesapi.yhgl.service.YhKhjbxxService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/api/userprofilemgt/dacustomerffgx")
@Api(tags = "用户管理-付费关系接口(easy)")
public class BwMeterFfgxEasyController {

    @Autowired
    private  YhKhjbxxService yhKhjbxxService;

    @ApiOperation(value = "新增付费关系(批量)", tags = {"付费关系"})
    @PostMapping("/saveBatch")
    public ResultRespone saveBatch(@RequestBody Map<String, Object> data) throws Exception {
        String customerNoZb = "";
        String customerNoFbList = "";
        String operatorCode = "";
        String operatorName = "";
        String operatorDatetime = "";
        try {
            customerNoZb = data.get("yhbhZb").toString();
            customerNoFbList = data.get("yhbhFbList").toString();
            if (data.get("czrybm") != null) operatorCode = data.get("czrybm").toString();
            if (data.get("czryxm") != null) operatorName = data.get("czryxm").toString();
            if (data.get("czsj") != null) {
                operatorDatetime = data.get("czsj").toString();
            } else {
                operatorDatetime = DateUtils.formatDate_pattern(new Date(), "yyyy-MM-dd HH:mm:ss");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultRespone.failed(e.getMessage());
        }
        //验证
        if (StringUtils.isBlank(customerNoZb)) {
            return ResultRespone.failed("主户用户编号不能为空！");
        }
        if (StringUtils.isBlank(customerNoFbList)) {
            return ResultRespone.failed("分户用户编号数量不能为0！");
        }

        DbHelper db = DbHelper.getDbHelper();
        String strSql = MessageFormat.format("delete from yh_ffgx where yhbh_fb in(''{0}'')", customerNoFbList.replace(",", "','"));
        db.addTransactionSql(strSql);
        strSql = "insert into yh_ffgx select sys_guid(),yhbh_zb,yhbh_fb,cbkid_id_zb,cbkid_id_fb,'{2}','{3}',{4} from (\n" +
                "  select '1' k, yhbh as yhbh_zb,null as cbkid_id_zb from yh_khjbxx where yhbh='{0}'\n" +
                ") zb inner join (\n" +
                "  select '1' k, yhbh as yhbh_fb,null as cbkid_id_fb from yh_khjbxx where yhbh in('{1}')\n" +
                ") fb on(zb.k=fb.k)";
        strSql = MessageFormat.format(strSql.replace("'", "''"), customerNoZb, customerNoFbList.replace(",", "','"), operatorCode, operatorName, SqlConvertor.toDate(operatorDatetime));
        db.addTransactionSql(strSql);

        boolean result = db.executeTransaction();
        if (!result) {
            return ResultRespone.failed("数据处理失败！");
        }
        return ResultRespone.success(result);
    }

    @GetMapping("/page")
    @ApiOperation(value = "付费关系组查询（分页）", tags = {"付费关系"})
    public ResultRespone<PageResult<YhkhjbxxFfgxzSelect>> getPage(@ModelAttribute PageDto pageDto, @ModelAttribute YhkhjbxxFilter filter) {
        return ResultRespone.success(yhKhjbxxService.getFfgxPage(pageDto, filter));
    }
}
